Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

switching between tabs in vim with vim-airline

I am using vim-airline plugin which works pretty good however, the only way to switch between tabs is to user :bp or :bn which is shortcuts for :bprevious or :bnext. The problem with this is that if I am on first tab and want to switch to the last tab i.e. 10th tab then I have to type :bn ten times to get there. How can I switch directly? Maybe something pressing arrow keys would be beneficial.

like image 793
Om3ga Avatar asked Feb 28 '15 08:02

Om3ga


People also ask

How do I switch between 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).

How do I switch between Nerdtree tabs?

Ctrl + → arrow will switch to tab that is on the right of current tab.

How do I go to a specific tab in Vim?

If you're really meticulous and want to position tabs just so in Vim, you can move the tabs to a specific spot in the tab order using :tabm n , where n is the position number that you want to use. If you don't give the :tabm command an argument, then the current tab will be moved to the last spot.


4 Answers

if you are using vim-airline, I'm strongly suggested that to using AirlineSelectTab command, you can see it via :help airline, below is the configuration.

  let g:airline#extensions#tabline#buffer_idx_mode = 1
  nmap <leader>1 <Plug>AirlineSelectTab1
  nmap <leader>2 <Plug>AirlineSelectTab2
  nmap <leader>3 <Plug>AirlineSelectTab3
  nmap <leader>4 <Plug>AirlineSelectTab4
  nmap <leader>5 <Plug>AirlineSelectTab5
  nmap <leader>6 <Plug>AirlineSelectTab6
  nmap <leader>7 <Plug>AirlineSelectTab7
  nmap <leader>8 <Plug>AirlineSelectTab8
  nmap <leader>9 <Plug>AirlineSelectTab9
  nmap <leader>0 <Plug>AirlineSelectTab0
  nmap <leader>- <Plug>AirlineSelectPrevTab
  nmap <leader>+ <Plug>AirlineSelectNextTab
like image 105
ruinb0w Avatar answered Sep 23 '22 09:09

ruinb0w


Your problem is that you installed a plugin designed to obfuscate Vim's regular commands and workflows without even knowing those regular ways. The shortcut you took lead you to a dead-end so your only reasonable solution is to go back to the main road and learn Vim properly, without training wheels and crutches.

So… from your question, it seems you are using Airline's so-called "smarter tabline" which displays your open buffers in a fake tabline.

If it was a real tabline, they would actually be tab pages and you would move between them with their own set of commands.

But they are buffers and yes, you are supposed to move between them with these commands:

:bnext
:bprevious
:bfirst
:blast
:b10
:b <buffer-name>

which can all be mapped for your convenience, of course.

But… that plugin doesn't show buffer numbers, so you can't use :b10 to jump reliably to the tenth buffer in your fake "tabline" so that's one less tool in your tool-belt.

And some special buffers, like the quickfix list, can be reached with :bn/:bn without — probably — being listed in your fake "tabline" so that makes your fake "tabline" a rather poor abstraction, even without considering the glaring limitations of tabs in general.

And there's the idiosyncratic behavior of that fake "tabline" which becomes a semi-real "tabline" when you actually use tab pages.

Conflating two very different — and powerful in their own ways — concepts into a single bastardized one is not really a good idea.

I suggest you disable that option and use buffers and tab pages as they are meant to be used.

Reference:

:help buffers
:help tab-page
like image 36
romainl Avatar answered Oct 23 '22 14:10

romainl


Agree with @romainl but you can always map your +tab to :bn or :bp for your ease.

" Tab navigation like Firefox. nnoremap <C-S-tab> :bprevious<CR> nnoremap <C-tab> :bnext<CR>

like image 7
Rafi Avatar answered Oct 23 '22 14:10

Rafi


In addition to Rafi's answer, put this in your .vimrc to get direct access to a buffer / airline tab.

nmap <leader>1 :bfirst<CR>
nmap <leader>2 :bfirst<CR>:bn<CR>
nmap <leader>3 :bfirst<CR>:2bn<CR>
nmap <leader>4 :bfirst<CR>:3bn<CR>
...

Alternatively, you can double down on airline with

let g:airline#extensions#tabline#buffer_idx_mode = 1
nmap <leader>1 <Plug>AirlineSelectTab1
nmap <leader>2 <Plug>AirlineSelectTab2
like image 5
Titou Avatar answered Oct 23 '22 12:10

Titou