2

vim provides a nice way to complete the Ex commands, which uses the -complete=custom,Func or complete=customlist,Func. It also provides many predefined completion methods such as -complete=dir.

Is is it possible to reuse these predefined completion methods in a user defined complete method? For example, I want to first extract the list of possible command and suffix them with a user defined string suffix. Example:

command -nargs=1 -complete=customlist,UserComplete CreateGitFile
function! UserComplete(ArgLead, CmdLine, CursorPos)
    let list = GetCompletionListFor_Dir()   " <--- what to do here
    let list1 = map(copy(list), 'v:val . "/.gitignore"')
    let list2 = map(copy(list), 'v:val . "/.gitconfig"')
    return list1 + list2
endfunction

1 Answer 1

3

Try getcompletion():

:echo getcompletion('*', 'dir')
                     │    │
                     │    └ looking for completion of directories
                     │
                     └ no filtering

Used in your code, it would give:

command -nargs=1 -complete=customlist,UserComplete CreateGitFile
function! UserComplete(ArgLead, CmdLine, CursorPos)
    let list = getcompletion('*', 'dir')
    let list1 = map(copy(list), 'v:val . "/.gitignore"')
    let list2 = map(copy(list), 'v:val . "/.gitconfig"')
    return list1 + list2
endfunction

See :h getcompletion().


More generally, when you're looking for a built-in function, whose exact name you don't know yet, have a look at :h function-list.
Or, on the command-line, use wildcards surrounding a relevant keyword.
For example, in your issue, you're looking for a function which gives completion candidates, so it should probably contain compl.
In this case, type this command:

:h *compl*()

But don't execute it, press C-d instead of Enter so that Vim lists all functions whose name contains compl. It should give:

:h *compl*()
complete()        complete_add()    getcompletion()   complete_check()

From there, you only have 4 functions to check in the help.

4
  • 1
    Thank you very much. Especially for the way to get relevant help information for vim. I always have trouble to look up the help.
    – doraemon
    Commented Sep 10, 2018 at 14:32
  • when I use getcompletion('~/', 'file'), the list returned contains items with ~/ expanded to /home/username/. The behavior differs from the behavior of completing :!ls ~/. Is there a way to keep the ~/ path instead of /home/username/?
    – doraemon
    Commented Sep 10, 2018 at 15:27
  • 1
    You could use a combination of map() and substitute(). For example: :echo map(getcompletion('~/', 'file'), {i,v -> substitute(v, '\m\C^'.$HOME.'/', '~/', '')})
    – user938271
    Commented Sep 10, 2018 at 15:38
  • 1
    @LiuSha Please read the help at :h help-summary. It provides many hints on how to find specific things. Commented Sep 21, 2018 at 5:54

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.