57

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.

1 Answer 1

78

Use <f-args>:

command! -nargs=1 MyCommand call s:MyFunc(<f-args>)
Sign up to request clarification or add additional context in comments.

1 Comment

I wasn't sure what the ! meant... it means overwrite existing command. I found this helpful: adp-gmbh.ch/vim/user_commands.html > :com! Existing-cmd-name cmd-replacement

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.