VIM: How to pass arguments to functions from user commands?

Vim

Vim Problem Overview


I am trying to create a user-defined command in VIM that takes one argument and calls a function with the user supplied argument. Seems simple but I am unable to get it to work. Here is the code from my foo.vim plugin:

function! s:MyFunc(myParam)
    do something
endfunction

command! -nargs=1 MyCommand call s:MyFunc(myParam)

When I try this out in a VIM buffer like this:

:MyCommand exampleParam

I get the following errors:

E121: Undefined variable: myParam 
E116: Invalid arguments for function <SNR>7_MyFunc

What's wrong here? How do I fix this? Any help is greatly appreciated.

Vim Solutions


Solution 1 - Vim

Use <f-args>:

command! -nargs=1 MyCommand call s:MyFunc(<f-args>)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionvenkView Question on Stackoverflow
Solution 1 - VimJSBձոգչView Answer on Stackoverflow