Tabpage mappings from my personal Vim config
I use tabpages quite heavily in my personal workflow.
When I’m reading code, I’ll create a new tabpage before I jump to a definition, which lets me follow a codepath freely without losing track of where I was before.
If I’m working in a codebase that follows a microservices architecture, I like to have a separate tabpage open for each service that I’m currently interested in.
I also use tabpages when I’m reviewing code. Using vim-fugitive’s :G difftool -y command, I can open a bunch of diffs, each in a different tabpage, for all of the files in a PR.
If, like me, you make heavy use of tabpages, it’s probably a good idea to have a set of mappings set up for working with them. Here’s a few from my personal Vim config that I can’t live without.
A shortcut for :tabclose
We’ve already seen how we can use :tabclose to close the current tabpage, or :4tabclose to close tabpage 4. If you use :tabclose often, you might find this mapping useful.
" [count]<C-w>C is a shortcut for :[count]tabclose.
nnoremap <silent> <C-w>C :<C-u>exe (v:count ? v:count : '') . 'tabclose'<cr>
If you add that to your Vim configuration, you can use <C-w>C in normal mode to close the current tabpage. You can also use [count]<C-w>C (again in normal mode) to close the [count]’th tabpage.
This does not shadow any existing keys, so you’re not losing existing functionality with this.
A shortcut for :tabonly
You can use :tabonly to close down all but the current tabpage. You can also use :4tabonly to close down all but tabpage 4. If this is a key part of your workflow, consider adding the following mapping:
" [count]<C-w>O is a shortcut for :[count]tabonly.
nnoremap <silent> <C-w>O :<C-u>exe (v:count ? v:count : '') . 'tabonly'<cr>
This does not shadow any existing keys, so you’re not losing existing functionality with this.
A shortcut for :tab sp
We already know that we can use :tab sp to “duplicate” the current window into a new tabpage. You can also use <C-w>T to “rip out” the current window into a tabpage of its own. I much prefer the :tab sp behaviour over the <C-w>T behaviour, though — I often want to “focus in” on a window temporarily without disturbing my current window layout. If you’re like me, and you’d prefer <C-w>T to leave the existing window alone, check out this mapping:
" [count]<C-w>T is overridden to perform :[count - 1]tab sp. This behaves in a
" similar way to the vanilla <C-w>T, but it doesn't close down any existing
" windows. I prefer this behaviour.
nnoremap <silent> <C-w>T :<C-u>exe (v:count ? v:count - 1 : '') . 'tab sp'<cr>
This does shadow the existing <C-w>T mapping, but I much prefer this behaviour.