Vim Tips Wiki
(Move categories to tip template)
(Remove html character entities)
Line 3: Line 3:
 
|id=1386
 
|id=1386
 
|previous=1385
 
|previous=1385
|next=1387
+
|next=1388
 
|created=November 13, 2006
 
|created=November 13, 2006
 
|complexity=intermediate
 
|complexity=intermediate
Line 23: Line 23:
 
The next enhancement is the following mapping:
 
The next enhancement is the following mapping:
 
<pre>
 
<pre>
:inoremap &lt;expr&gt; &lt;cr&gt; pumvisible() ? "\&lt;c-y&gt;" : "\&lt;c-g&gt;u\&lt;cr&gt;"
+
:inoremap <expr> <CR> pumvisible() ? "\<c-y>" : "\<c-g>u\<CR>"
 
</pre>
 
</pre>
   
The above mapping will change the behavior of the "&lt;Enter&gt;" key when the popup menu is visible. In that case the "&lt;Enter&gt;" key will simply select the highlighted menu item, just as the "Ctrl-y" key does.
+
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 the "Ctrl-y" key does.
   
 
These two mappings are probably the most rare, yet most valuable:
 
These two mappings are probably the most rare, yet most valuable:
 
<pre>
 
<pre>
:inoremap &lt;expr&gt; &lt;c-n&gt; pumvisible() ? "\&lt;lt&gt;c-n&gt;" : "\&lt;lt&gt;c-n&gt;\&lt;lt&gt;c-r&gt;=pumvisible() ? \"\\&lt;lt&gt;down&gt;\" : \"\"\&lt;lt&gt;cr&gt;"
+
:inoremap <expr> <c-n> pumvisible() ? "\<lt>c-n>" : "\<lt>c-n>\<lt>c-r>=pumvisible() ? \"\\<lt>down>\" : \"\"\<lt>cr>"
   
:inoremap &lt;expr&gt; &lt;m-;&gt; pumvisible() ? "\&lt;lt&gt;c-n&gt;" : "\&lt;lt&gt;c-x&gt;\&lt;lt&gt;c-o&gt;\&lt;lt&gt;c-n&gt;\&lt;lt&gt;c-p&gt;\&lt;lt&gt;c-r&gt;=pumvisible() ? \"\\&lt;lt&gt;down&gt;\" : \"\"\&lt;lt&gt;cr&gt;"
+
:inoremap <expr> <m-;> pumvisible() ? "\<lt>c-n>" : "\<lt>c-x>\<lt>c-o>\<lt>c-n>\<lt>c-p>\<lt>c-r>=pumvisible() ? \"\\<lt>down>\" : \"\"\<lt>cr>"
 
</pre>
 
</pre>
   
In the above mappings, the first will make "Ctrl-n" work the way it normally does; however, when the menu appears, the "&lt;down&gt;" 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 "Ctrl-x, Ctrl-o" to bring up the omni completion menu, then it simulates "Ctrl-n, Ctrl-p" to remove the "longest" common text, and finally it simulates "&lt;down&gt;" again to keep a match highlighted.
+
In the above mappings, the first will make "Ctrl-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 "Ctrl-x, Ctrl-o" to bring up the omni completion menu, then it simulates "Ctrl-n, Ctrl-p" to remove the "longest" common text, and finally it simulates "<Down>" again to keep a match highlighted.
   
 
See [[VimTip1228]]
 
See [[VimTip1228]]

Revision as of 02:54, 29 September 2008

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 Ctrl-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 the "Ctrl-y" key does.

These two mappings are probably the most rare, yet most valuable:

:inoremap <expr> <c-n> pumvisible() ? "\<lt>c-n>" : "\<lt>c-n>\<lt>c-r>=pumvisible() ? \"\\<lt>down>\" : \"\"\<lt>cr>"

:inoremap <expr> <m-;> pumvisible() ? "\<lt>c-n>" : "\<lt>c-x>\<lt>c-o>\<lt>c-n>\<lt>c-p>\<lt>c-r>=pumvisible() ? \"\\<lt>down>\" : \"\"\<lt>cr>"

In the above mappings, the first will make "Ctrl-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 "Ctrl-x, Ctrl-o" to bring up the omni completion menu, then it simulates "Ctrl-n, Ctrl-p" to remove the "longest" common text, and finally it simulates "<Down>" again to keep a match highlighted.

See VimTip1228

Comments

Merge with VimTip1228?

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)