switching between tabs in vim with vim-airline
Asked Answered
J

6

13

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.

Jehiel answered 28/2, 2015 at 8:32 Comment(1)
Shouldn't this belong @ vi.stackexchange.com?Manwell
K
14

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
Kansas answered 28/2, 2015 at 9:52 Comment(4)
Thank you for answer however, after reading :help tab-page I couldn't find convenient way of switching between pages.Jehiel
That's because, thanks to that plugin author's weird ideas about user experience, you confuse tab pages and buffers. If you use buffers, use buffer commands (:help buffers); if you use tab pages, use tab pages commands (:help tab-page).Kansas
in vim, tabs are not the tabs you know from sublimetext or any other editor. to put it simply: buffers = all your open files. tabs = a certain number of buffers.Johannejohannes
If you're willing to invoke 2 commands then you can reliably jump to the tenth buffer in your airline using :bfirst followed by :10bn.Lyingin
H
8

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>

Hammett answered 29/6, 2016 at 10:1 Comment(1)
This does not work in text-mode vim (e.g., via SSH) because Ctrl-Tab cannot be mapped. However, it does work in gVim.Zipnick
A
5

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
Aruba answered 12/9, 2018 at 7:51 Comment(0)
K
5

If you are using vim-airline, it just exposes <Plug>AirlineSelectTab keys and it's for you to map them to convenient key sequences:

  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

About mapping see e.g. https://vim.fandom.com/wiki/Mapping_keys_in_Vim_-Tutorial(Part_1)

Kyanize answered 8/6, 2021 at 2:24 Comment(0)
P
2

I have configured vim to switch between tabs using Ctrl + arrow keys.

Ctrl + will switch to tab that is on the left of current tab.

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

Ctrl + will switch to first tab.

Ctrl + will switch to last tab.

To achieve above behaviour update your vimrc with following lines:

nnoremap <C-Left> :tabprevious<CR>
nnoremap <C-Right> :tabnext<CR>
nnoremap <C-Up> :tabfirst<CR>
nnoremap <C-Down> :tablast<CR>

This works with vim-airline plugin as well.

Reference: Using vim tab pages

Pachalic answered 24/7, 2021 at 11:40 Comment(0)
V
1

If you are using tabs instead of buffers

" Tab navigation (works only in gvim, not in console)
nnoremap <C-tab>   gt
nnoremap <C-S-tab> gT
Violetavioletta answered 4/6, 2020 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.