I have the following in my init.vim

"If PUM (complete menu) is visible, then execute <C-y> (which selects an item), otherwise, do a normal tab  
inoremap <silent><expr> <TAB>
            \pumvisible() ? "\<C-n>" : "\<TAB>"
inoremap <silent><expr> <S-Tab>
            \pumvisible() ? "\<C-p>" : "\<S-TAB>"

But for some reason, this doesn't seem to be working in insert mode. In command mode, I'm getting the expected behaviour. I'm tabbing through the suggestions and the currently selected item fills itself into the command line. But in insert mode, when I press tab I'm getting 2 spaces inserted instead. My normal tabbing inserts 4 spaces FYI.

Not sure what to change here. I've looked through conflicting mappings in init.vim and don't see anything.

I got the above code snippet from these two questions:

Cycle through autocomplete menu using tab

Neovim: Select an autocomplete option with <tab> instead of <return>

  • Either you're misunderstanding your mappings or there is some conflict in your configuration. Try to do the same after running nvim -u NONE and setting inoremap <expr> <Tab> pumvisible() ? "<C-n>" : "<Tab>". Press <C-n> to show the autocompletion suggestions and press <Tab>. That should work as intended, but you need to have the popup menu visible as a prerequisite for this mapping to work.
    – r_31415
    Jun 2 at 19:08
  • You're right, this was a mapping conflict, but I have no idea what. After this didn't work, I set it within COC settings, and after that, when i came back to this above snippet again, it seems to be working. No idea what i changed between then and now.
    – Jay Pillai
    Jun 2 at 22:47
  • I'm glad you figured out there was indeed a conflict. It is a good idea to take some time to trim down your init.vim so that this sort of thing is unlikely to happen again (e.g. maybe switching to neovim's built-in LSP if you really need it and rely on native autocompletion :h ins-completion)
    – r_31415
    Jun 2 at 23:00
  • Yeah, setting up native LSP was how i got into the conflict in the first place. I switched to COC and had no issues since it's an out of the box solution. Now i'm scrubbing the whole init and porting over everything to lua. I'll be more careful about mappings this time
    – Jay Pillai
    Jun 2 at 23:03
  • 1
    I started with a vim-bootstrap config and i'm finding it harder and harder to keep fixing a prebuilt config. Much better to start from scratch and know what all my bindings are so i don't keep running into conflicts.
    – Jay Pillai
    Jun 3 at 13:51


Your Answer

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

Browse other questions tagged or ask your own question.