Vim Tips Wiki
Register
Advertisement
Tip 1221 Printable Monobook Previous Next

created 2006 · complexity basic · author Kim Schulz aka KimuSan · version 7.0


Vim 7 introduced tab pages with functions to navigate to different tabs (see using tab pages or :help gt or :help :tab). You might however want to map some of those functions to separate keys. To do so, add the desired snippet to your vimrc.


Some of the mappings used here interfere with Vim's normal procedures. For example:

Some mappings might not work in terminal versions of Vim (especially mappings, that use the Alt key or a combination of Ctrl and Shift).

Tab navigation using the the leader key and number row[]

nnoremap <leader>1 1gt
nnoremap <leader>2 2gt nnoremap <leader>3 3gt nnoremap <leader>4 4gt nnoremap <leader>5 5gt ...

Vim navigation[]

nnoremap th  :tabfirst<CR>
nnoremap tk  :tabnext<CR>
nnoremap tj  :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>

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. You can also use Ctrl-PageDown and Ctrl-PageUp to cycle through tabs which works by default in gvim and Firefox.

For opening and closing tabs, you can also add Ctrl-Insert and Ctrl-Delete mappings like this:

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

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

Vim navigation[]

nnoremap th  :tabfirst<CR>
nnoremap tk  :tabnext<CR>
nnoremap tj  :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 H and L to move to the previous/next tabpage.

nnoremap H gT
nnoremap L gt

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

Comments[]

Advertisement