When wondering about the standard, good way of writing something in VimL I usually check two main resources: the Vim documentation, which is extremely comprehensive and well written, and Steve Losh's blogs (Learn Vimscript the Hard Way and Writing Vim Plugins).
One approach to find examples in the Vim documentation is finding the entry on the subject and them check the start of that file; for :help :command
:
This subject is introduced in sections |05.3|, |24.7| and |40.1| of the user
manual.
Searching the file for the 40.1 section:
The <f-args> keyword contains the same information as the <args> keyword,
except in a format suitable for use as function call arguments. For example:
>
:command -nargs=* DoIt :call AFunction(<f-args>)
:DoIt a b c
It shows something similar to what you guessed: calling separated functions, but with arguments.
Unfortunately I couldn't find anything about this subject on the blogs mentioned above.
As was mentioned on the question comments, the alternative is to use \
as line continuation, but that usually leads to clumsy and hard-to-read/edit code.
There is a pattern that I have seen many times when people use the function approach and the <bang>
which is made available by :command
:
command -bang MyCmd call MyFunction(<bang>0, arg2)
This is useful because <bang>
expands on !
or nothing, so concatenating it with 0
makes it easier to check with an if
.
Edit:
As mentioned by Carpetsmoker, it is better to use script-local s:a_function()
instead of a global AFunction()
, as it is more localized and doesn't appear in tab completions and such.
\
at the start of the continuation line(s).fun! s:my_fun()
, this will only be visible inside the current file (and is not "exposed" needlessly when running Vim). I don't like the\
line-continuation and|
to chain commands as I find them unreadable and awkward to deal with.:h command-completion-custom
:)<f-args>
(see:h <f-args>
)