Vim Tips Wiki
(<C-n> auto-select tip adapted for searching backwards too)
(Forgot bullet point.)
Line 52: Line 52:
 
:into my files each time I press Enter when there's no pop up menu.
 
:into my files each time I press Enter when there's no pop up menu.
 
::Works for me! What version of Vim do you have? It looks like your Vim is ignoring the <tt><nowiki><expr></nowiki></tt> tag and using the mapping literally instead of evaluating it as an expression first. {{help|prefix=no|:map-expression}}s are a feature introduced in Vim 7, and I do not see any compile-time option for removing them. --[[User:Fritzophrenic|Fritzophrenic]] 20:05, February 22, 2011 (UTC)
 
::Works for me! What version of Vim do you have? It looks like your Vim is ignoring the <tt><nowiki><expr></nowiki></tt> tag and using the mapping literally instead of evaluating it as an expression first. {{help|prefix=no|:map-expression}}s are a feature introduced in Vim 7, and I do not see any compile-time option for removing them. --[[User:Fritzophrenic|Fritzophrenic]] 20:05, February 22, 2011 (UTC)
:If you like the part of the tip where <C-n> automatically selects the first entry by simulating <Down>, you can add the same thing for simulating <Up> when searching backwards with <C-p>:
+
*If you like the part of the tip where <C-n> automatically selects the first entry by simulating <Down>, you can add the same thing for simulating <Up> when searching backwards with <C-p>:
 
<code>
 
<code>
 
<pre>
 
<pre>

Revision as of 08:38, 1 March 2011

Tip 1386 Printable Monobook Previous Next

created November 13, 2006 · complexity intermediate · author Matt Zyzik · version n/a


In most IDEs, you normally type some code, press <C-Space> for a completion popup menu, type some more characters to select the menu item you want, then hit <Enter> to insert that completion into the code. With Vim's initial popup menu settings, the behavior of the popup menu is a little less pleasant (for some people).

The first step to "improve" the menu behavior is to execute this command:

:set completeopt=longest,menuone

The above command will change the 'completeopt' option so that Vim's popup menu doesn't select the first completion item, but rather just inserts the longest common text of all matches; and the menu will come up even if there's only one match. (The longest setting is responsible for the former effect and the menuone is responsible for the latter.)

The next enhancement is the following mapping:

:inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"

The above mapping will change the behavior of the <Enter> key when the popup menu is visible. In that case the Enter key will simply select the highlighted menu item, just as <C-Y> does.

These two mappings further improve the completion popup menu:

inoremap <expr> <C-n> pumvisible() ? '<C-n>' :
  \ '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'

inoremap <expr> <M-,> pumvisible() ? '<C-n>' :
  \ '<C-x><C-o><C-n><C-p><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'

In the above mappings, the first will make <C-N> work the way it normally does; however, when the menu appears, the <Down> key will be simulated. What this accomplishes is it keeps a menu item always highlighted. This way you can keep typing characters to narrow the matches, and the nearest match will be selected so that you can hit Enter at any time to insert it. In the above mappings, the second one is a little more exotic: it simulates <C-X><C-O> to bring up the omni completion menu, then it simulates <C-N><C-P> to remove the longest common text, and finally it simulates <Down> again to keep a match highlighted.

Comments

 TO DO 

I don't know. Tip 1228 doesn't explain what their mappings do at all, and there are a lot more of them. This tip tells what's going on, although admittedly it does not tell how it works (which I would like).
--Fritzophrenic 17:12, 19 November 2007 (UTC)
  • Further explanation?
  • Broken mapping?
The <CR> mapping doesn't work - it causes vim to insert

pumvisible() ? "\" : "\
"
into my files each time I press Enter when there's no pop up menu.
Works for me! What version of Vim do you have? It looks like your Vim is ignoring the <expr> tag and using the mapping literally instead of evaluating it as an expression first. :map-expressions are a feature introduced in Vim 7, and I do not see any compile-time option for removing them. --Fritzophrenic 20:05, February 22, 2011 (UTC)
  • If you like the part of the tip where <C-n> automatically selects the first entry by simulating <Down>, you can add the same thing for simulating <Up> when searching backwards with <C-p>:

" keep menu item always highlighted by simulating <Up> on pu visible
inoremap <expr> <C-p> pumvisible() ? '<C-p>' :
  \ '<C-p><C-r>=pumvisible() ? "\<lt>Up>" : ""<CR>'