How to make shortcut for :tabnew, :tabn, :tabp?
In vim, I'd like to shorten :tabnew
to :tn
, :tabp
to :th
, :tabn
to :tl
somewher开发者_开发百科e in my .vimrc
. Any idea how I would remap commands like this?
Use cabbrev:
ca tn tabnew
ca th tabp
ca tl tabn
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>
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.
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
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).
精彩评论