Vim map a key combination while in insert mode

vim

This is probably an easy question for most vim users.

I want to map :tabn to <C-Tab>. It works perfectly while I'm in normal mode but when I'm in my insert mode (typing code) it just inserts tabs characters.

In my vimrc file I have

map <C-Tab> :tabn<CR>

Any ideas?
Thanks!

Best Answer

Try this:

:imap <C-Tab> <Esc>:tabn<CR>

That will leave you in normal mode after switching to the next tab. You could instead use this:

:imap <C-Tab> <C-O>:tabn<CR>

which will put you back into insert mode in the next tab at the position where you left the cursor in that tab.

Related Question