completion 

Send to Kindle
home » snippets » vim » completion


Pages
example_1.py        



Plugins

 

command completion

 

Use the following when defining the command to enable completion.

-complete=customlist,{func} custom completion, defined via {func}

The {func} part should be a function with the following signature:

:function {func}(ArgLead, CmdLine, CursorPos)

The function should return the completion candidates as a Vim List. Non-string items in the list are ignored. Vim will not filter the returned completion candidates.

argument description
ArgLead the leading portion of the argument currently being completed on
CmdLine the entire command line
CursorPos the cursor position in it (byte index)

The following example completes filenames from the directories specified in the 'path' option:

command -nargs=1 -bang -complete=customlist,EditFileComplete
       \ EditFile edit<bang> <args>
function EditFileComplete(A,L,P)
    return split(globpath(&path, a:ArgLead), "\n")
endfunction

completefunc and omnifunc.

 

The function is called in two different ways:

  • First the function is called to find the start of the text to be completed.
  • Later the function is called to actually find the matches.

First Invocation

 

On the first invocation the arguments are:

a:findstart  1
a:base      empty
  • The function must return the column where the completion starts. It must be a number between zero and the cursor column "col('.')".
  • The text between this column and the cursor column will be replaced with the matches.
  • Return -1 if no completion can be done.

Second Invocation

 
a:findstart  0
a:base      the text with which matches should match; the text that was
            located in the first call (can be empty)
  • The function must return a List of complete-items.
    • These matches usually include the "a:base" text.
  • When there are no matches return an empty List.

complete-items

 

Each list item can either be a string or a Dictionary. When it is a string it is used as the completion. When it is a Dictionary it can contain these items:

key value description
word string the text that will be inserted, mandatory
abbr string abbreviation of "word"; when not empty it is used in the menu instead of "word"
menu string extra text for the popup menu, displayed after "word" or "abbr"
This is used in the popup menu and may be truncated.
thus it should be relatively short.
info string More info. Can be long. Displayed in a preview window
when "preview" appears in 'completeopt'
Use a single space to remove existing text in the preview window.
kind string single letter indicating the type of completion
icase bool when non-zero case is to be ignored when comparing items to be equal;
when omitted zero is used, thus items that only differ in case are added
dup string when non-zero this match will be added even when an item with the same word is already present.

The "kind" item uses a single letter to indicate the kind of completion. This may be used to show the completion differently (different color or icon). Currently these types can be used:

type description
v variable
f function or method
m member of a struct or class
t typedef
d #define or macro

complete_add and complete_check

 
  • When searching for matches takes some time call |complete_add()| to add each match to the total list. These matches should then not appear in the returned list!
  • Call |complete_check()| now and then to allow the user to press a key while still searching for matches. Stop searching when it returns non-zero.

An example that completes the names of the months:

fun! CompleteMonths(findstart, base)
  if a:findstart
    " locate the start of the word
    let line = getline('.')
    let start = col('.') - 1
    while start > 0 && line[start - 1] =~ '\a'
      let start -= 1
    endwhile
    return start
  else
    " find months matching with "a:base"
    let res = []
    for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
      if m =~ '^' . a:base
    call add(res, m)
      endif
    endfor
    return res
  endif
endfun
set completefunc=CompleteMonths

The same, but now pretending searching for matches is slow:

fun! CompleteMonths(findstart, base)
  if a:findstart
    " locate the start of the word
    let line = getline('.')
    let start = col('.') - 1
    while start > 0 && line[start - 1] =~ '\a'
      let start -= 1
    endwhile
    return start
  else
    " find months matching with "a:base"
    for m in split("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec")
      if m =~ '^' . a:base
    call complete_add(m)
      endif
      sleep 300m    " simulate searching for next match
      if complete_check()
    break
      endif
    endfor
    return []
  endif
endfun
set completefunc=CompleteMonths