Quantcast
Channel: Active questions tagged user-commands - Vi and Vim Stack Exchange
Viewing all articles
Browse latest Browse all 60

How to define a command if the function needs to take an argument from the script itself?

$
0
0

Suppose I am defining a few commands that are similar in functionality. In this particular case, I am defining a few commands that each echoes a particular message:

function! EchoMessage(text)    echom textendfunctionlet thousand_chars = "天地玄黃,宇宙洪荒。日月盈昃,辰宿列張。寒來暑往,秋收冬藏。閏餘成歲,律呂調陽 ..."let lorem_ipsum = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, ..."let brown_fox = "The quick brown fox jumps over the lazy dog."command! ThousandCharacters call EchoMessage(thousand_chars)command! LoremIpsum call EchoMessage(lorem_ipsum)command! BrownFox call EchoMessage(brown_fox)

I have written it in this way to be able to reuse EchoMessage for many commands. Unfortunately, an error is produced when any of the commands (e.g. :LoremIpsum) are called, because vim will not provide text when EchoMessage is called using a command.

How can I structure my code so that the commands can make use of a common function (e.g. EchoMessage) with one argument provided in the script itself (e.g. brown_fox)?


Viewing all articles
Browse latest Browse all 60

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>