sponsor Vim development Vim logo Vim Book Ad

basic Tip #1228: Completion popup selection like other IDEs

 tip karma   Rating 41/14, Viewed by 4000 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   May 11, 2006 11:54      complexity:   basic
author:   Gerald Lai      as of Vim:  

Vim 7's popup menu for completion (omni or otherwise) takes a little getting used to when it comes to selecting the completed word. See ":help ins-completion". The completed word can be selected using the arrow keys, PageUp/Down, Enter and Esc. In addition to that, there is Ctrl-E, Ctrl-Y, Ctrl-N and Ctrl-P.

Here's an alternative setup that feels a little more like the completion menu of other IDEs. Place in vimrc.

  inoremap <silent><Esc>      <C-r>=pumvisible()?"\<lt>C-e>":"\<lt>Esc>"<CR>
  inoremap <silent><CR>       <C-r>=pumvisible()?"\<lt>C-y>":"\<lt>CR>"<CR>
  inoremap <silent><Down>     <C-r>=pumvisible()?"\<lt>C-n>":"\<lt>Down>"<CR>
  inoremap <silent><Up>       <C-r>=pumvisible()?"\<lt>C-p>":"\<lt>Up>"<CR>
  inoremap <silent><PageDown> <C-r>=pumvisible()?"\<lt>PageDown>\<lt>C-p>\<lt>C-n>":"\<lt>PageDown>"<CR>
  inoremap <silent><PageUp>   <C-r>=pumvisible()?"\<lt>PageUp>\<lt>C-p>\<lt>C-n>":"\<lt>PageUp>"<CR>

Use up/down arrow and PageUp/Down keys to scroll through the popup menu. The word in text will change as you scroll through. Press Enter to accept the selected word. Hit Esc to cancel completion and go back to the original word.

For those of you who prefer Esc to work like Enter, you can use this:

  inoremap <silent><Esc>      <C-r>=pumvisible()?"\<lt>C-y>":"\<lt>Esc>"<CR>

 rate this tip  Life Changing Helpful Unfulfilling 

<< Vim-Shell: Embedded full-strength terminal in vim | Map F9 for convinient debugging VIM plugins >>

Additional Notes

hari_vim at yahoo dot com, May 11, 2006 12:48
You can also use the new <expr> to make these slightly more simpler:

inoremap <expr> <Esc>      pumvisible()?"\<C-E>":"\<Esc>"
inoremap <expr> <CR>       pumvisible()?"\<C-Y>":"\<CR>"
inoremap <expr> <Down>     pumvisible()?"\<C-N>":"\<Down>"
inoremap <expr> <Up>       pumvisible()?"\<C-P>":"\<Up>"
inoremap <expr> <PageDown> pumvisible()?"\<PageDown>\<C-P>\<C-N>":"\<PageDown>"
inoremap <expr> <PageUp>   pumvisible()?"\<PageUp>\<C-P>\<C-N>":"\<PageUp>"
Anonymous, May 11, 2006 13:22
Up/Down/PgUp/PgDn should all work fine out of the box
[email protected], May 11, 2006 14:10
Cool!
But how stop completion by first right variant. I wana like in other IDEs just see popup with variants and complete only after chose and <enter> (popup content filtered-as-type).
Suresh Govindachar, May 11, 2006 18:03
veged,

  Map however you get into completion into something that
  gets you to completion in the state you want to be in.
  For example, use the following map to adjust the state
  you get into when looking for spelling suggestions using
  i_CTRL-X s:

             inoremap <C-X>s  <C-X>s<C-P><Down>

  
Anonymous, May 11, 2006 19:38
Perhaps you will find the following option useful
set completeopt+=longest
[email protected], May 11, 2006 22:06
For scrolling page up and page down, I also find this useful:

inoremap <expr> <C-D> pumvisible()?"\<PageDown>\<C-P>\<C-N>":"\<C-D>"
inoremap <expr> <C-U> pumvisible()?"\<PageUp>\<C-P>\<C-N>":"\<C-U>"
Gerald Lai, May 12, 2006 12:29
Thanks Hari :) It'll take a little while for me to adjust from the Vim 6 way of doing things.
Anonymous, May 13, 2006 16:28
I just want to be able to use j and k to browse through the suggestion list. Unbelievable that they made it so I have to take my hands off the home row to use the autocompletion...
Anonymous, May 13, 2006 17:48
You still can, but will be restricted from typing those letters once the popup menu is up.

inoremap <expr> j     pumvisible()?"\<C-N>":"j"
inoremap <expr> k     pumvisible()?"\<C-P>":"k"
Anonymous, June 5, 2006 16:07
you can also choose the highlighted word with just <space> to select this word and append space after it
[email protected], June 10, 2006 7:16
Hi, you know how change the color of this pop up ?
[email protected], June 29, 2006 17:25
For color:

highlight   Pmenu               term=NONE cterm=NONE ctermfg=7 ctermbg=5 gui=NONE guifg=White guibg=Magenta
highlight   PmenuSel            term=NONE cterm=NONE ctermfg=0 ctermbg=7 gui=NONE guifg=Black guibg=White
highlight   PmenuSbar           term=NONE cterm=NONE ctermfg=7 ctermbg=0 gui=NONE guifg=White guibg=Black
highlight   PmenuThumb          term=NONE cterm=NONE ctermfg=0 ctermbg=7 gui=NONE guifg=Black guibg=White

If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo