How do I create a custom completion pop up menu.

I would like to be able to begin typing a part of a string then press a custom key combination to bring up a pop up completion of just a certain group of strings.

I already have omni complete set up and do not want to add to that list.

For example I have the list:

I would like to be able to type sp then press ctrl+> and have a completion box come up for just that specific list.

  inoremap <F5> <C-R>=ListMonths()<CR>

    func! ListMonths()
      call complete(col('.'), ['January', 'February', 'March',
            \ 'April', 'May', 'June', 'July', 'August', 'September',
            \ 'October', 'November', 'December'])
      return ''

Found it! Tucked away in that Grimoire they are trying to tell us are docs =)

======================= My final script is something like

inoremap <F5> <C-R>=CustomComplete()<CR>

        func! CustomComplete()
                echom 'move to start of last word'
                normal b
                echom 'select word under cursor'
                let b:word = expand('<cword>')
                echom '->'.b:word.'<-'
                echom 'save position'
                let b:position = col('.')
                echom '->'.b:position.'<-'
                normal e
                normal l
                echom 'move to end of word'     

                let b:list = ["spoogle","spangle","frizzle"]
                let b:matches = []

                echom 'begin checking for completion'
                for item in b:list
                echom 'checking '
                echom '->'.item.'<-'      
                        echom 'adding to matches'
                        echom '->'.item.'<-'      
                        call add(b:matches,item)
                call complete(b:position, b:matches)
                return ''
  • I dare you to find any documentation as complete as the Vim documentation though.
    – Swiss
    May 22 '12 at 5:39
  • Vim is madness. Complete insanity what you can do with it. May 22 '12 at 5:56

Create a list of words: $HOME/wordlist and add the dictionary to vim.

:set dictionary+=$HOME/wordlist

Activate the word list:

:set complete-=k complete+=k

Now you can autocomplete the words with <CTRL>-P and <CTRL-N

Edit: To have separate word lists, you create multiple lists, e.g.


and assign key bindings to pick a particular work list:

noremap <F5> :set dictionary=$HOME/firstlist<CR>:set complete-=k complete+=k<CR>
noremap <F6> :set dictionary=$HOME/secondlist<CR>:set complete-=k complete+=k<CR>

This has the advantage that you can use the <CTRL>-P and <CTRL>-N shortcuts as you're used to, once you picked the desired list.

  • Can I have separate dictionaries though? I would like a hot key for only a specific list. I am already using a dictionary. May 22 '12 at 3:02
  • @JamesAndino I updated my answer.
    – Marco
    May 22 '12 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.