20

In vim, I'd like to shorten :tabnew to :tn, :tabp to :th, :tabn to :tl somewhere in my .vimrc. Any idea how I would remap commands like this?

2
  • 4
    Note that :tabn can already be achieved by gt in normal mode, and likewise :tabp by gT. 1gt will go to the first tab, 2gt will go to the second, and so on. Aug 5, 2012 at 15:54
  • I was after a :te -> :tabedit shortcut. This comment is to help people searching for that to find this question. Jun 2, 2015 at 15:15

7 Answers 7

27

Use cabbrev:

ca tn tabnew
ca th tabp
ca tl tabn
3
  • ah that is really cool, when you go :tn then hit space, it auto-completes :tabnew
    – tester
    Jul 10, 2011 at 0:42
  • 1
    This is awesome. I was unaware of cabbrev.
    – zachwill
    Jul 10, 2011 at 4:46
  • 7
    Note that cabbrev expands abbreviations everywhere on the command line, i.e. when you enter :%e th.txt, the th will be expanded to tabp. Jun 24, 2013 at 7:24
7

you can add the following code in to the ~/.vimrc file and navigate through the tabs every easily.

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>
1
  • 3
    Just note that this overrides many uses of "t" (which finds a characters and places your cursor just before it - a variation of "f"). I mention this because I used to use these mapping until I realized I wanted "t" back. Aug 13, 2017 at 14:45
6

Daniel Kullmann points out the currently accepted answer is dangerous.

If you use ca tn tabnew, anytime you type the string th, it can expand unexpectedly.

For example, :!ls /tmp/tn/ will expand into :!ls /tmp/tabnew/

The approach listed in this answer does not suffer the same problem. Using it would look like this:

cnoreabbrev <expr> tn getcmdtype() == ":" && getcmdline() == 'tn' ? 'tabnew' : 'tn'
cnoreabbrev <expr> th getcmdtype() == ":" && getcmdline() == 'th' ? 'tabp' : 'th'
cnoreabbrev <expr> tl getcmdtype() == ":" && getcmdline() == 'tl' ? 'tabn' : 'tl'
cnoreabbrev <expr> te getcmdtype() == ":" && getcmdline() == 'te' ? 'tabedit' : 'te'

Those customizations ensure the expansion is done only on the commands and nowhere else.

1
  • 4
    The last :te shortcut for :tabedit wasn't part of the original question. I added it since searching for that answer was how I got to this page. Hopefully it'll help others get here faster. Jun 2, 2015 at 15:18
4

There is better way to navigate among tabs. Just try (C is for Control):

nmap <silent> <C-n> :tabnext<CR>
nmap <silent> <C-p> :tabprev<CR>
imap <silent> <C-n> <esc><C-n>
imap <silent> <C-p> <esc><C-p>
1
  • You can do this with "gt" (tabnext) and "gT" (tabprev).
    – erny
    Apr 26, 2013 at 8:20
4
"To create a new tab
nnoremap <C-t> :tabnew<Space>
inoremap <C-t> <Esc>:tabnew<Space>

"Tab Navigation
nnoremap <S-h> gT
nnoremap <S-l> gt
1
  • 1
    I just updated it like this: nnoremap <C-t> :tabnew<CR> and inoremap <C-t> <Esc>:tabnew<CR>. To open tab without pressing Enter.
    – Ihor
    Oct 28, 2015 at 9:39
3

If you want to keep the same mapping that is suggested here, https://stackoverflow.com/a/17269521/2743772, and don't want to use other suggestions, try adding leader to the beginning and this way it doesn't overwrite "t", unless of course you already have these exact mappings for something else.

nnoremap <Leader>th  :tabfirst<CR>
nnoremap <Leader>tj  :tabnext<CR>
nnoremap <Leader>tk  :tabprev<CR>
nnoremap <Leader>tl  :tablast<CR>
nnoremap <Leader>tt  :tabedit<Space>
nnoremap <Leader>tn  :tabnext<Space>
nnoremap <Leader>tm  :tabm<Space>
nnoremap <Leader>td  :tabclose<CR>
1

Ctrl + PageUp and Ctrl + PageDown move between tabs by default.

The shortcuts must not be bind by the terminal for this to work. (I am on Ubuntu 18.04).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.