Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch to specific tab in Vim like in Firefox?

Tags:

vim

How do I make Vim switch to a certain tab when I hit Alt+# ? For example, to get to the third tab, I'd hit Alt+3 (like the behavior in Firefox).

Edit: also, how can I make Control + tab == gt, Control + shift + tab == gT

like image 741
victor Avatar asked Jun 03 '09 00:06

victor


People also ask

How do I switch tabs in Vim?

To switch to the next tab, use :tabn, and to switch to the previous tab, use :tabp (short for tabnext and tabprevious respectively). You can also jump over tabs by using :tabn 2, which will move to the second next tab. To jump to the first tab, use :tabr (tabrewind) and to jump to the last tab use :tabl (tablast).

What is a tab in Vim?

The easiest way to think about tab pages in Vim is to consider them to be viewports, layouts, or workspaces. In many editors (not Vim), each file is opened in a new tab, and one tab can show only one file, and one file cannot appear in more than one tab.


3 Answers

:nmap <M-1> :tabnext 1<CR>
:nmap <M-2> :tabnext 2<CR>
etc.

See :h :tabnext. Note that by default you can also do Ngt in normal mode where N is the number of the tab you want (starting with 1).

like image 72
Brian Carper Avatar answered Sep 26 '22 00:09

Brian Carper


This works for me (copied and pasted from my rc):

" Tab Control (others)
 map <A-1> 1gt
 map <A-2> 2gt
 map <A-3> 3gt
 map <A-4> 4gt
 map <A-5> 5gt
 map <A-6> 6gt
 map <A-7> 7gt
 map <A-8> 8gt
 map <A-9> 9gt

Also further goodness:

map <C-Right> <ESC>:tabnext<CR>
map <C-Left> <ESC>:tabprev<CR>
map <C-t> <ESC>:tabnew<CR>

You may want to change it to nmap like the example above to restrict the usage a little better I've been a bit lazy in that respect.

Missed your edit next tab with Ctl-t would be:

map <C-t> :tabnext<CR>

I don't think the tabprevious mapping is possible in vim due to the way it handles uppercase characters see:

http://www.nabble.com/Maping-Ctrl-Shift-s-problems-td22918941.html

To save some time I spent hunting around when I wanted this

like image 38
Ben Avatar answered Sep 27 '22 00:09

Ben


Getting this to work in urxvt proved difficult. Eventually I settled on using the folowing bindings:

:nnoremap <Esc>1 gt1
:nnoremap <Esc>2 gt2
:nnoremap <Esc>3 gt3
:nnoremap <Esc>4 gt4
:nnoremap <Esc>5 gt5
:nnoremap <Esc>6 gt6
:nnoremap <Esc>7 gt7
:nnoremap <Esc>8 gt8
:nnoremap <Esc>9 gt9
:nnoremap <Esc>0 gt0

The problem was that Alt number combinations get bound to escape prefixed numbers by default in rxvt. Originally I managed by having the following in my ~/.vimrc file:

:nnoremap <M-1> gt1
:nnoremap <M-2> gt2
...

This was made to work by using the following to '~/.Xdefaults':

URxvt*meta8: true

This forces rxvt to use the 8th character bit when Alt is pressed, which is what Vim uses to detect the Alt state. Setting this in your .Xdefaults file enables the 8th bit to be set. However, this causes problems in other programs, such as irssi running over screen, hence my suggested solution.

like image 43
Nicolas Wu Avatar answered Sep 26 '22 00:09

Nicolas Wu