Sunday, November 28, 2010

Alternative tab navigation

Here is an alternative way to open and navigate tabs in Vim 7. Add the following to your vimrc:

" tab navigation like firefox
:nmap <C-S-tab> :tabprevious<CR>
:nmap <C-tab> :tabnext<CR>
:map <C-S-tab> :tabprevious<CR>
:map <C-tab> :tabnext<CR>
:imap <C-S-tab> <Esc>:tabprevious<CR>i
:imap <C-tab> <Esc>:tabnext<CR>i
:nmap <C-t> :tabnew<CR>
:imap <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.

CommentsEdit  href= Edit


map th :tabfirst<CR>
map tj :tabnext<CR>
map tk :tabprev<CR>
map tl :tablast<CR>
map tt :tabedit<Space>
map tn :tabnext<Space>
map tm :tabm<Space>

They are quite like the mappings below, by leading the combinations with the easily-reached/rarely-used "t" key. I use "hjkl" to navigate as they are "left/down/up/right" for vi users. "jk" (down/up) match the directions when using a mouse to scroll through tabs, and "hl" (left/right) stand for leftmost/rightmost tabs.

I ended up with:

map th :tabnext<CR>
map tl :tabprev<CR>
map tn :tabnew<CR>
map td :tabclose<CR>

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

:nmap <C-S-t> :tabnew<CR>
:imap <C-S-t> <Esc>:tabnew<CR>
:imap <C-S-w> <Esc>:tabclose<CR>

Didn't exactly work in Windows' gvim. Had to put it into _gvimrc, like this:

:map <C-S-tab> :tabprevious<CR>
:map <C-tab> :tabnext<CR>
:map <C-t> :tabnew<CR>

Why not add Ctrl-w to close tabs?

:map <C-w> :tabclose<CR>

A: because <Ctrl-w> is the start of the window commands - like <C-w>n for new window...

I just use the way that Vim provides and it work well:

  • gt for tabnext
  • gT for tabprevious


:map <S-h> gT
:map <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'.

<C-tab> , <C-S-tab> don't work on Eterm, aterm, xterm

We can use <C-PageDown> and <C-PageUp> instead of <C-tab> and <C-S-tab>
This's the default way in Vim 7. By the way, this also work on Firefox.

This tip is really good. But the key sequences used for switching/creating the tabs is clashing with other features of Vim. For example:

  • CTRL+T is used for jumping to previous tags [exuberant ctags].
  • CTRL+W is used for jumping to next split window in multiple windows
  • CTRL+TAB is captured by KDE itself to switch workspaces.

Can you give a nice key combinations which will work with all these features ?. I know tabs is a new feature of vim7. But this script can become awesome if worked properly on.

Vim 7 already has <C-PageUp> and <C-PageDown> to cycle through tabs. To open and close them I added

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

(not for insert or visual mode because they do something else then).

I know that Z and X both perform functions in normal mode, but I never use them, so I just mapped:

:nmap Z :tabprev<CR>
:nmap X :tabnext<CR>

That way shift-z and shift-x takes me forward and backward. It's a lazy, ergonimic one hand solution.

I found this tip

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

I neglected to RTFM. The key combination is <tab number>gt. Here's a handy alternative to switch to tab-N by pressing Alt-FN

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

I found <C-tab> wasn't picked up on the Solaris machine I was working on, so I used function keys.

:map  <F9>  :tabnew      <CR>
:map  <F10> :tabclose    <CR>
:map  <F11> :tabprevious <CR>
:map  <F12> :tabnext     <CR>
:nmap <F9>  :tabnew      <CR>
:nmap <F10> :tabclose    <CR>
:nmap <F11> :tabprevious <CR>
:nmap <F12> :tabnext     <CR>
:imap <F9>  :tabnew      <CR>
:imap <F10> :tabclose    <CR>
:imap <F11> :tabprevious <CR>
:imap <F12> :tabnext     <CR>
" F10 = VMS for exit ;-)


No comments: