Remap <Tab> while completion popup is visible

438 views
Skip to first unread message

Matteo Landi

unread,
Jan 4, 2014, 8:47:29 AM1/4/14
to vim...@googlegroups.com

Hi Everyone,

I would like to remap the <Tab> key so that vim will automatically pick the current selected item from the completion popup.  I thought it would have been easy but was wrong;  here is what I added to my .vimrc file:

  inoremap <expr> <Tab> pumvisible() ? "\<C-Y>" : "\<Tab>"

Vim still keeps adding tabs rather than closing the completion popup.  Is it something wrong with that?  Maybe other plugins interfering?  I even found at least two different resources where something like this is done, but none of them worked.


Cheers,

Matteo

[1] http://vim.wikia.com/wiki/Make_Vim_completion_popup_menu_work_just_like_in_an_IDE
[2] https://github.com/Shougo/neocomplete.vim

ZyX

unread,
Jan 5, 2014, 1:03:26 PM1/5/14
to vim...@googlegroups.com
On Saturday, January 4, 2014 5:47:29 PM UTC+4, Matteo Landi wrote:
> Hi Everyone,
>
> I would like to remap the <Tab> key so that vim will automatically pick the current selected item from the completion popup.  I thought it would have been easy but was wrong;  here is what I added to my .vimrc file:
>
>   inoremap <expr> <Tab> pumvisible() ? "\<C-Y>" : "\<Tab>"
>
> Vim still keeps adding tabs rather than closing the completion popup.  Is it something wrong with that?  Maybe other plugins interfering?  I even found at least two different resources where something like this is done, but none of them worked.

Works for me. Can you show what exactly did you type, what you expected to see as a result and what you saw.

Note that `"\<C-y>"` here actually produces string `"\"\\\<C-y>\""` instead of `"\"\\<C-y>\""`. I.e. <C-y> gets replaced with control character *before* string evaluation happens meaning that backslashes are useless here. Since backslash-escaped control character inside double quotes is just control character this does not do any harm *except* for misguiding reader about <C-y> handling in this case: it is handled when doing :map argument expansion and not expression evaluation.

Matteo Landi

unread,
Jan 5, 2014, 3:13:48 PM1/5/14
to vim...@googlegroups.com
Imagine I just typed `Hea' and the completion popup pops in with the wollowing words: Head, Hearst, Heath, Heather and Heaviside.  To autocomplete with `Heater' I would move the selection to the right line using arrow keys or <C-N>/<C-P> and then would confirm the action with either Enter or <C-Y>.

What happens with the mapping above?  When I press <Tab> to confirm the autocompletion, Vim closes the completion popup and insert a space at the end -- leaving me with `Hea ' instead of `Heather'.

Is there a way to check for any interfering plugin?  Maybe vim-autocomplpop?


Cheers



--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Paul

unread,
Jan 7, 2014, 9:25:18 AM1/7/14
to vim...@googlegroups.com
On Saturday, 04 January, 2014 at 13:47:29 GMT, Matteo Landi wrote:
> inoremap <expr> <Tab> pumvisible() ? "\<C-Y>" : "\<Tab>"

I have this, which works for me:

inoremap <expr><tab> pumvisible() ? "\<down>" : "\<tab>"
inoremap <expr><s-tab> pumvisible() ? "\<up>" : "\<s-tab>"

Ben Fritz

unread,
Jan 7, 2014, 11:09:43 AM1/7/14
to vim...@googlegroups.com
On Sunday, January 5, 2014 2:13:48 PM UTC-6, Matteo Landi wrote:
> Imagine I just typed `Hea' and the completion popup pops in

By default, Vim will only pop up a completion menu if you explicitly tell it to, by pressing one of the completion key combinations: <C-N>, <C-P>, <C-X><C-O>, etc. If your menu pops up automatically as you type, it must be a plugin doing that.

> with the wollowing words: Head, Hearst, Heath, Heather and Heaviside.  To autocomplete with `Heater' I would move the selection to the right line using arrow keys or <C-N>/<C-P> and then would confirm the action with either Enter or <C-Y>.
>
>
>
> What happens with the mapping above?  When I press <Tab> to confirm the autocompletion, Vim closes the completion popup and insert a space at the end -- leaving me with `Hea ' instead of `Heather'.
>
>
>
> Is there a way to check for any interfering plugin?  Maybe vim-autocomplpop?
>

You can check to see what mapping is actually active for the <Tab> key, and what file defined that mapping:

Insert and command-line mode:
:verbose map! <Tab>

Normal, operator pending, and visual mode:
:verbose map <Tab>

If these commands show your mapping, then there must be some problem with the mapping or Vim settings interfering. If these commands show some other mapping, check the file it says created the mapping.

Note, if you have the 'paste' option set, your insert-mode mappings will not be used. I'd double-check that option value before doing anything else.

Matteo Landi

unread,
Jan 7, 2014, 3:23:41 PM1/7/14
to vim...@googlegroups.com
Thank you Ben, your suggestion -- :verbose map <Tab>, helped me figure out the problem.  There was indeed another interfering mapping and once removed everything worked like a charm.


Cheers.

Reply all
Reply to author
Forward
0 new messages