4

I have a serious problem. Usually, the editor I use is vim, though I can't map ctrl+tab action. I wanna switch tabs with ctrl+tab (ctrl + shift + tab). In xterm, this mapping is enabled, and the action will be used. But, in gnome-terminal, tab is escape sequence have just appeared. Please tell me how to switch tabs in vim with gnome-terminal.

3 Answers 3

7

Due to the way that the keyboard input is handled internally, this unfortunately isn't generally possible today, even in GVIM. Some key combinations, like Ctrl + non-alphabetic cannot be mapped, and Ctrl + letter vs. Ctrl + Shift + letter cannot be distinguished. (Unless your terminal sends a distinct termcap code for it, which most don't.) In insert or command-line mode, try typing the key combination. If nothing happens / is inserted, you cannot use that key combination. This also applies to <Tab> / <C-I>, <CR> / <C-M> / <Esc> / <C-[> etc. (Only exception is <BS> / <C-H>.) This is a known pain point, and the subject of various discussions on vim_dev and the #vim IRC channel.

Some people (foremost Paul LeoNerd Evans) want to fix that (even for console Vim in terminals that support this), and have floated various proposals, cp. http://groups.google.com/group/vim_dev/browse_thread/thread/626e83fa4588b32a/bfbcb22f37a8a1f8

But as of today, no patches or volunteers have yet come forward, though many have expressed a desire to have this in a future Vim 8 major release.

3

The problem is that in a terminal, a Tab character is ^I (Control-I). This means that pressing control while pressing tab is not something the terminal even bothers to pass through to Vim.[1] It just sends a regular tab character.

GVim can support this because it doesn't have to rely on the terminal to tell it what keys are being pressed in what combination--it has more direct access to keyboard events, so it has no trouble seeing modified special keys.

I suggest you find a different key to map so you can have consistency.

[1] It is my understanding that with "modern" terminal emulators this is no longer true, but it was true for so long that there is a huge legacy hurdle to overcome because the terminal library that Vim uses (Ncurses) doesn't even handle this event, so even though the terminal technically may support it, Vim can't because the library API it relies on can't. Roughly once a year there's been serious discussion in the Vim Developers' list about fixing this problem by various means, including switching to a more capable API, but nothing ever comes of it.

1
  • 1
    It's still true with most terminal emulators – neither xterm, gnome-terminal (vte), nor urxvt handle Ctrl+Tab any differently from Tab.
    – user1686
    Jul 24, 2014 at 8:17
1

The closest answer I have for this is to create Leader + Tab and Leader + Shift + Tab

map <leader><Tab> :bnext<cr>
map <leader><S-Tab> :bprevious<cr>

I have mapped my Leader to , (comma), so it's typing , + Tab

However they don't work very well. You can't keep Leader held down and keep hitting Tab to scroll through the tabs. This is also not in the MRU (most recently used) behaviour either.

For clarification I'm using 'buffers' instead of 'tab pages' as I find these map much better to the concept of what other editors use tabs for.

Instead of this, you can try the following:

map <leader><Tab> :b<Space>

This opens up the ex command, and then you can press Tab to start scrolling through the buffers. You can also then use and to go back and forth.

With this approach you can also start typing the part of an open buffer and pressing Tab will then cycle through the filtered list of open buffers.

Personally though I just map Leader + Tab to give me the basic MRU behaviour:

map <leader><Tab> :b#<cr>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.