Vim Tips Wiki
m (reviewed. the tip is still relevant and good.)
Tags: Visual edit apiedit
No edit summary
Tag: sourceedit
Line 6: Line 6:
 
|complexity=basic
 
|complexity=basic
 
|author=Kim Schulz aka KimuSan
 
|author=Kim Schulz aka KimuSan
|version=n/a
+
|version=7.0
 
|rating=459/143
 
|rating=459/143
 
|category1=Tabs
 
|category1=Tabs
 
|category2=
 
|category2=
 
}}
 
}}
Vim 7 introduces {{help|id=new-tab-pages|prefix=no|label=tab pages}} into vim and adds functions for navigating to different tabs (e.g. {{help|prefix=no|gt}} or {{help|prefix=no|:tab}}). You might however want to map some of those functions to separate keys. To do so, add the desired snippet to your vimrc.
+
Vim 7 introduced {{help|id=new-tab-pages|prefix=no|label=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.
   
  +
Please note, that some of the mappings used here, might interfere with existing functionalities, like
 
  +
Some of the mappings used here interfere with Vim's normal procedures. For example:
* {{help|prefix=no|CTRL-T}} is used for jumping to previous tags
 
* CTRL+W is used as a prefix for the window commands
+
* {{help|prefix=no|Ctrl-T}} is used for jumping to previous tags
  +
* Ctrl+W is used as a prefix for the {{help|id=CTRL-W|prefix=no|label=window commands}}
* CTRL+TAB is captured by KDE itself to switch workspaces.
+
* Ctrl+Tab is captured by KDE 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)
+
Some mappings might not work in terminal versions of Vim (especially mappings, that use the Alt key or a combination of Ctrl and Shift).
   
 
==Firefox like navigation==
 
==Firefox like navigation==
 
<pre>
 
<pre>
" tab navigation like firefox
+
" Tab navigation like Firefox.
 
nnoremap <C-S-tab> :tabprevious<CR>
 
nnoremap <C-S-tab> :tabprevious<CR>
 
nnoremap <C-tab> :tabnext<CR>
 
nnoremap <C-tab> :tabnext<CR>
Line 30: Line 31:
 
</pre>
 
</pre>
   
You can open a new tab with ctrl-t, go forward through the tabs with ctrl-tab
+
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.
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>
+
For opening and closing tabs, you can also add Ctrl-Insert and Ctrl-Delete mappings like this:
mappings like this:
 
 
<pre>
 
<pre>
 
nnoremap <C-Insert> :tabnew<CR>
 
nnoremap <C-Insert> :tabnew<CR>
 
nnoremap <C-Delete> :tabclose<CR>
 
nnoremap <C-Delete> :tabclose<CR>
 
</pre>
 
</pre>
Note, that those keys are only used in normal mode, because in insert and visual mode they already have a function.
+
Note, that those keys are only mapped in normal mode, because in insert and visual mode they already have a function.
   
==Vi navigation==
+
==Vim navigation==
 
<pre>
 
<pre>
 
nnoremap th :tabfirst<CR>
 
nnoremap th :tabfirst<CR>
Line 59: Line 55:
 
"nnoremap tn :tabnew<CR>
 
"nnoremap tn :tabnew<CR>
 
</pre>
 
</pre>
Those mappings use the easily-reached "t" key in combination with the well known "{{help|prefix=no|h}}{{help|prefix=no|j}}{{help|prefix=no|k}}{{help|prefix=no|l}}" 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.
+
Those mappings use the easily-reached '''t''' key in combination with the well known {{help|prefix=no|h}}{{help|prefix=no|j}}{{help|prefix=no|k}}{{help|prefix=no|l}} 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==
+
==Gnome-terminal navigation==
 
For something like gnome-terminal tab-related key shortcuts:
 
For something like gnome-terminal tab-related key shortcuts:
 
<pre>
 
<pre>
Line 76: Line 72:
 
</pre>
 
</pre>
   
The second autocommand creates a new last tabpage for any buffer that is
+
The second autocommand creates a new last tabpage for any buffer that is created (e.g. when using <tt>:e foobar</tt>, 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.)
created (e.g. when using <tt>:e foobar</tt>, 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==
 
==Use <A-Fn> to go to the nth tabpage==
Line 97: Line 90:
   
 
==Other customization==
 
==Other customization==
Use &lt;S-h> and &lt;S-l> to move to the previous/next tabpage.
+
Use H and L to move to the previous/next tabpage.
 
<pre>
 
<pre>
nnoremap <S-h> gT
+
nnoremap H gT
nnoremap <S-l> gt
+
nnoremap L gt
 
</pre>
 
</pre>
   
That way you can hold down the shift key while you scroll left and right through the tabs with 'h' and 'l'.
+
That way you can hold down the Shift key while you scroll left and right through the tabs with '''h''' and '''l'''.
  +
  +
==Comments==

Revision as of 04:07, 18 September 2015

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:

  • 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 to switch workspaces.

Some mappings might not work in terminal versions 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. 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 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 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