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 text

let 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)?

1 Answer 1


The problem is that, in the body of your EchoMessage() function, text does not refer to the value passed in, but instead to a different local variable. Use a: to refer to parameters passed in:

echom a:text

See :help internal-variables (and specifically :help function-argument) for more details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.