vim cant map to :tabnext

2019-02-06 21:13发布

问题:

I have the following mappings in my .vimrc:

map <C-S-Tab> :tabprevious<CR>
nmap <C-S-Tab> :tabprevious<CR>
imap <C-S-Tab> <Esc>:tabprevious<CR>i

map <C-Tab> :tabnext<CR>
nmap <C-Tab> :tabnext<CR>
imap <C-Tab> <Esc>:tabnext<CR>i

I want to switch the tabs with Strg+Tab forward and with Strg+Shift+Tab backward. Why does this mapping not work?

回答1:

Are you using xterm? If so, you can't map ctrl-tab without a lot of hackery. xterm and many other terminal emulators don't recognise ctrl-tab and simply send a tab character.

See my answer here for more details: Mapping <C-Tab> in my vimrc fails in Ubuntu

Or you can just use gvim if that is suitable - it should work without any mucking around.



回答2:

Something is probably blocking vim from seeing the C-Tab. This could be your terminal or your window manager.

On some OSes/WMs you can set exceptions to the window manager shortcuts, but how you do this varies crazily between the WMs.

I'm not sure if there is a solution if it is your terminal blocking the key presses.



回答3:

oh ... is it need to be mapped? just use this predefined combos:

  • gt: == :tabnext

  • gT: == :tabprevious

  • [n]gt: == jump to N tab , e.g. 1gt , 3gt

defining to many short keys is easy, but how can you remember all of them and don't mixing them up with 'ctrl + tab' , 'alt + tab' ( common and usual short keys in most OS )

check this page for more details : http://vim.wikia.com/wiki/Alternative_tab_navigation



回答4:

All you need is xterm.

Put this in your .Xresources file (you can copy-paste in this case):

xterm*VT100.Translations: #override \
             Ctrl ~Shift <Key>Tab: string(0x1b) string("[27;5;9~") \n\
             Ctrl Shift <Key>Tab: string(0x1b) string("[27;6;9~")

Then do cd ; xrdb .Xresources and restart xterm.


Put this in your .vimrc:

!! Important - instead of XXXX you must type CTRL-V and then Esc OR copy-paste the whole text and run %s/\(set <F1[34]>=\)XXXX/\=submatch(1) . "\33"/g which is copy-pastable (insert it with <CTRL-R> +).

set timeout timeoutlen=1000 ttimeoutlen=100
set <F13>=XXXX[27;5;9~
nnoremap <F13> gt
set <F14>=XXXX[27;6;9~
nnoremap <F14> gT

And restart vim.

Done.