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?
7 Answers
Use cabbrev:
ca tn tabnew
ca th tabp
ca tl tabn
-
ah that is really cool, when you go
:tn
then hit space, it auto-completes :tabnew– testerJul 10, 2011 at 0:42 -
1
-
7Note that cabbrev expands abbreviations everywhere on the command line, i.e. when you enter
:%e th.txt
, theth
will be expanded totabp
. Jun 24, 2013 at 7: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>
-
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. Aug 13, 2017 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.
-
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. Jun 2, 2015 at 15:18
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>
"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
-
1I just updated it like this:
nnoremap <C-t> :tabnew<CR>
andinoremap <C-t> <Esc>:tabnew<CR>
. To open tab without pressing Enter.– IhorOct 28, 2015 at 9:39
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>
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).
:tabn
can already be achieved bygt
in normal mode, and likewise:tabp
bygT
.1gt
will go to the first tab,2gt
will go to the second, and so on.:te
->:tabedit
shortcut. This comment is to help people searching for that to find this question.