There are many build targets in the project, I would like to create a command, for example BuildFor. The idear is, the alternative parameter will present one by one while keep pressing the Tab key after entering the command in Vim's command line window. Each predefined parameter represents one building target which will eventually be constructed as a ninja/GNU Build within the function that is called by the BuildFor command.


How can I create a sort of enum to save all those options for the BuildFor command and make them presented by tapping Tab key.


Any better build integration suggestions for Vim you know that is better than this, I would be very appreciate you can share.

1 Answer 1


You need to write your own complete function. Like the following:

let s:env = [ "test", "perf", "preprod", "prod" ]

command! -complete=customlist,ListFor -nargs=1 BuildFor make <args>
function! ListFor(argLead, line, pos)
    return filter(s:env, { i, v -> match(v, '^' . a:argLead) == 0})

If you the enter:

:BuildFor pr<TAB>

Vim will propose the completions "preprod" and "prod".

If the completion depends on other arguments already on the command line, you have to work with the function parameters line and pos to determine them.

Read about the details with :help :command-completion-custom.

  • Thanks Ralf, I've saw the initial names before function and arguments with at many places, for example, the s:env and v:val and a:argLead here, what is the purpose of those prefix? I guess this is for a name space?
    – SLN
    Feb 26, 2019 at 9:36
  • 1
    @SLN Yes, they are name-spaces. See :help internal-variables. I did a minor change in the call to match(..): Now using v instead of v:val (see :help lambda).
    – Ralf
    Feb 26, 2019 at 9:44

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.