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?
-
4Note 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 Answers
-
ah that is really cool, when you go `:tn` then hit space, it auto-completes :tabnew – tester Jul 10 '11 at 00:42
-
1
-
7Note that cabbrev expands abbreviations everywhere on the command line, i.e. when you enter `:%e th.txt`, the `th` will be expanded to `tabp`. – daniel kullmann Jun 24 '13 at 07:24
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>

- 818
- 11
- 10
-
3Just 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
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.

- 24,647
- 4
- 70
- 96
-
4The 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
"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

- 3,654
- 3
- 23
- 36
-
1I just updated it like this: `nnoremap
:tabnew – Ihor Oct 28 '15 at 09:39` and `inoremap :tabnew `. To open tab without pressing Enter.
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>

- 5,772
- 2
- 30
- 34
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>

- 379
- 4
- 24
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).

- 679
- 6
- 12