Vim Tips Wiki

Changes: Alternative tab navigation


Back to page

(removed dodgy category)
Line 1: Line 1:
{{dodgy|Remaps existing useful functionality, and there are ''way'' too many comments to wade through. Needs severe cleanup before being useful. Some of the mappings may be fairly useful and provide familiar keybindings to navigate through tabs...but do they really warrant an entire tip? Navigating tabs is easy anyway with builtin commands: gt and gT.}}

Revision as of 09:49, August 14, 2012

Tip 1221 Printable Monobook Previous Next

created 2006 · complexity basic · author Kim Schulz aka KimuSan · version n/a

Vim 7 introduces tab pages into vim and adds functions for navigating to different tabs (e.g. gt or :tab). You might however want to map some of those functions to separate keys. To do so, add the desired snippet to your vimrc.

Please note, that some of the mappings used here, might interfere with existing functionalities, like

  • CTRL-T is used for jumping to previous tags
  • CTRL+W is used as a prefix for the window commands
  • CTRL+TAB is captured by KDE itself to switch workspaces.

while other mappings might not work in terminal version of Vim (especially mappings, that use the Alt key or a combination of Ctrl and Shift)

Firefox like navigation

" tab navigation like firefox
nnoremap <C-S-tab> :tabprevious<CR>
nnoremap <C-tab>   :tabnext<CR>
nnoremap <C-t>     :tabnew<CR>
inoremap <C-S-tab> <Esc>:tabprevious<CR>i
inoremap <C-tab>   <Esc>:tabnext<CR>i
inoremap <C-t>     <Esc>:tabnew<CR>

You can open a new tab with ctrl-t, go forward through the tabs with ctrl-tab and backwards with ctrl-shift-tab. This way of navigating resembles the way it is done in Firefox. You can also use <C-PageDown> and <C-PageUp> to cycle through tabs which works by default in Vim and Firefox.

For opening and closing tabs, you can also add the <C-Insert> and <C-Delete> mappings like this:

nnoremap <C-Insert> :tabnew<CR>
nnoremap <C-Delete> :tabclose<CR>

Note, that those keys are only used in normal mode, because in insert and visual mode they already have a function.

Vi navigation

nnoremap th  :tabfirst<CR>
nnoremap tj  :tabnext<CR>
nnoremap tk  :tabprev<CR>
nnoremap tl  :tablast<CR>
nnoremap tt  :tabedit<Space>
nnoremap tn  :tabnext<Space>
nnoremap tm  :tabm<Space>
nnoremap td  :tabclose<CR>
" Alternatively use
"nnoremap th :tabnext<CR>
"nnoremap tl :tabprev<CR>
"nnoremap tn :tabnew<CR>

Those mappings use the easily-reached "t" key in combination with the well known "hjkl" navigation keys in normal mode that move the cursor left, down, up or right. 'tj' moves to the next tab, 'tk' moves to the previous tabpage while 'th' and 'tl' move to the leftmost/rightmost tabpage.

Gnome-Terminal navigation

For something like gnome-terminal tab-related key shortcuts:

:nnoremap <C-S-t> :tabnew<CR>
:inoremap <C-S-t> <Esc>:tabnew<CR>
:inoremap <C-S-w> <Esc>:tabclose<CR>

Open files always in new tabs

If you like to open all command line arguments in a new tabpage, use this snippet (but remember, that the 'tabpagemax' setting still applies).

autocmd VimEnter * tab all
autocmd BufAdd * exe 'tablast | tabe "' . expand( "<afile") .'"'

The second autocommand creates a new last tabpage for any buffer that is created (e.g. when using :e foobar, the current buffer will remain visible in the current tabpage and the file foobar will be opened in a new tabpage and Vim goes to that tabpage.)

Use <A-Fn> to go to the nth tabpage

You can use ngt to move to the nth tabpage. A handy alternative is to map the first 10 numbers to the Alt-F keys:

nnoremap <A-F1> 1gt
nnoremap <A-F2> 2gt
nnoremap <A-F3> 3gt
nnoremap <A-F4> 4gt
nnoremap <A-F5> 5gt
nnoremap <A-F6> 6gt
nnoremap <A-F7> 7gt
nnoremap <A-F8> 8gt
nnoremap <A-F9> 9gt
nnoremap <A-F0> 10gt

Other customization

Use <S-h> and <S-l> to move to the previous/next tabpage.

nnoremap <S-h> gT
nnoremap <S-l> gt

That way you can hold down the shift key while you scroll left and right through the tabs with 'h' and 'l'.

Around Wikia's network

Random Wiki