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?

kenorb
  • 155,785
  • 88
  • 678
  • 743
tester
  • 22,441
  • 25
  • 88
  • 128
  • 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. – Andrew Marshall Aug 05 '12 at 15:54
  • I was after a `:te` -> `:tabedit` shortcut. This comment is to help people searching for that to find this question. – Alan W. Smith Jun 02 '15 at 15:15

7 Answers7

27

Use cabbrev:

ca tn tabnew
ca th tabp
ca tl tabn
Sam Ruby
  • 4,270
  • 22
  • 21
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>
Harsha
  • 818
  • 11
  • 10
  • 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. – Ryan Bosinger Aug 13 '17 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.

Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
  • 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. – Alan W. Smith Jun 02 '15 at 15:18
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
ma08
  • 3,654
  • 3
  • 23
  • 36
  • 1
    I just updated it like this: `nnoremap :tabnew` and `inoremap :tabnew`. To open tab without pressing Enter. – Ihor Oct 28 '15 at 09:39
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>
taro
  • 5,772
  • 2
  • 30
  • 34
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>
ethikz
  • 379
  • 4
  • 24
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).

Olivier Lasne
  • 679
  • 6
  • 12