How to make shortcut for :tabnew, :tabn, :tabp? - vim

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?

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).

Related

My vim ui is weird. It is hard to explain in word, please see the picture

Help! My vim ui is weird. It is hard to explain in word, please see the picture.
Try cleaning the search register:
:let #/=""
I have some mapping to temporarelly disable hlsearch, actually it toggles it, wich is more convinient, in my humble opinion.
nnoremap <silent> <c-l> <ESC>:set hls! hls?<cr> :call clearmatches()<cr><left><c-l>
inoremap <silent> <c-l> <C-o>:set hls! hls?<cr> <C-o>:call clearmatches()<cr>
vnoremap <silent> <c-l> <ESC>:set hls! hls?<cr> <bar> gv :call clearmatches()<cr>

Vim <A-j> Keybinding for 10j moves cursor to the right

I've recently mapped 10j to <A-j> and 10k to <A-k>, which is seemingly quite amazing, but there is one problem with it:
When I normally type 10j (not using the shortcut), it will just move 10 rows down vertically but not move horizontally at all (given the lines have the same length), but when I use <A-j> it will always (well, interestingly enough, not always, but most of the times) also move one letter to the right.
Funnily enough, this happens only for <A-j>, whereas <A-k> works as intended. How can I prevent that? And maybe most importantly: Why is that?
If it helps, these are my other keybindings:
nnoremap K K<C-w>L
nnoremap <A-h> :set hls!<cr>
nnoremap / :set hlsearch<cr>/
nnoremap <A-j> 10j
nnoremap <A-k> 10k
nnoremap <A-w> W
nnoremap <A-b> B
nnoremap <A-v> V
nnoremap <A-m> '
nnoremap <A-p> "+p
nnoremap <A-y> "+y
nnoremap <A-4> $
nnoremap <A-3> 0
nnoremap Y y$
vnoremap <A-h> :set hls!<cr>
vnoremap / :set hlsearch<cr>/
vnoremap <A-j> 10j
vnoremap <A-k> 10k
vnoremap <A-w> W
vnoremap <A-b> B
vnoremap <A-v> V
vnoremap <A-m> '
vnoremap <A-p> "+p
vnoremap <A-y> "+y
vnoremap <A-4> $
vnoremap <A-3> 0
Yeah, I like the alt-key a lot.
You have a trailing space character at the end of your mapping:
:nnoremap <A-j>
n <M-j> * 10j<Space>
<Space> is the same command as l; it moves a character to the right (where possible).
The right-hand side in a mapping is taken literally (up to the end of the line or a | command separator). Another common mistake is appending a " comment to a mapping definition.
Plugin recommendations
If you regularly stumble over trailing whitespace (it's generally frowned upon in many coding styles, and tools like Git also highlight it as problematic), my ShowTrailingWhitespace plugin can alert you to those, and the DeleteTrailingWhitespace plugin can remove them for you. (The plugin pages have links to alternative plugins.)

use specific search with no highlight

For example I'm in python code and want to jump between classes:
nnoremap <buffer> [c /^\s*class\ <CR>
How to prevent them from highlight in more elegant way than :nohl at the end of command each time?
You can avoid highlighting search matches by using the :help search() function or writing your own function.
With search()
nnoremap <buffer> <silent> [c :<C-u>call search('^\s*\zsclass\s')<CR>
With your own function
" with ':help :normal'
function! JumpToNextClass()
normal! /^\s*\zsclass\s
endfunction
" with ':help search()'
function! JumpToNextClass()
call search('^\s*\zsclass\s')
endfunction
nnoremap <buffer> <silent> [c :<C-u>call JumpToNextClass()<CR>
But none of that really matters since Vim already comes with ]] and [[.

Vim: mapping to switch window and fill screen

I have in my .vimrc the following lines, which lets me switch windows with ctrl+hjkl:
nnoremap <C-h> <C-W>h
nnoremap <C-j> <C-W>j
nnoremap <C-k> <C-W>k
nnoremap <C-l> <C-W>l
These are fine for my desktop computer, but on my netbook, I want to have the active window completely fill the tiny screen. This means typing ctrl+w _ and ctrl+w | after each window change. The logical step would be to add those keystrokes to the mapping, yielding:
nnoremap <C-h> <C-W>h<C-W>_<C-W>|
nnoremap <C-j> <C-W>j<C-W>_<C-W>|
nnoremap <C-k> <C-W>k<C-W>_<C-W>|
nnoremap <C-l> <C-W>l<C-W>_<C-W>|
But that fails, consistently, when in a mapping, despite working when I simply type the required keys; and (as I have set 'showcmd') it seems to leave a trailing <C-W>.
I have also tried using :wincmd:
nnoremap <C-h> :wincmd h<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-j> :wincmd j<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-k> :wincmd k<cr>:wincmd _<cr>:wincmd |<cr>
nnoremap <C-l> :wincmd l<cr>:wincmd _<cr>:wincmd |<cr>
But that complains about trailing <cr> whenever my vimrc is sourced, so I'm not going to pursue that further without more research.
Any ideas?
Try using <Bar> instead of |. ie:
nnoremap <C-h> <C-W>h<C-W>_<C-W><Bar>
nnoremap <C-j> <C-W>j<C-W>_<C-W><Bar>
nnoremap <C-k> <C-W>k<C-W>_<C-W><Bar>
nnoremap <C-l> <C-W>l<C-W>_<C-W><Bar>
| are used to have multiply commands on one line and you will need to be escaped with a backslash when used literally:
nnoremap <C-h> <C-W>h<C-W>_<C-W>\|
nnoremap <C-j> <C-W>j<C-W>_<C-W>\|
nnoremap <C-k> <C-W>k<C-W>_<C-W>\|
nnoremap <C-l> <C-W>l<C-W>_<C-W>\|
On the other hand | can be useful:
nnoremap xxx :if 1 == 2 | echom "hello" | endif

Which custom key bindings do you use in Vim? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
Which custom key bindings do you use to increase productivity in Vim?
My two favorites are:
inoremap jj <Esc>
" Clear screen clears search highlighting.
nnoremap <C-L> :nohl<CR><C-L>
Window Management
" Window splitting
nmap <silent> <leader>sh :leftabove vnew<cr>
nmap <silent> <leader>sl :rightbelow vnew<cr>
nmap <silent> <leader>sk :leftabove new<cr>
nmap <silent> <leader>sj :rightbelow new<cr>
nmap <silent> <leader>swh :topleft vnew<cr>
nmap <silent> <leader>swl :botright vnew<cr>
nmap <silent> <leader>swk :topleft new<cr>
nmap <silent> <leader>swj :botright new<cr>
" Scroll the window next to the current one
" (especially useful for two-window splits)
nmap <silent> <leader>j <c-w>w<c-d><c-w>W
nmap <silent> <leader>k <c-w>w<c-u><c-w>W
Text Editing
" Toggle search highlighting
nmap <silent> <leader>/ :set hlsearch!<cr>
" Toggle paste mode
" (prefer this over 'pastetoggle' to echo the current state)
nmap <leader>p :setlocal paste! paste?<cr>
" Select the last edited (or pasted) text
nmap gv `[v`]
" Keep lines that do (or do not) contain the last search term
nmap <leader>v/ :v/<c-r>//d<cr>gg
nmap <leader>g/ :g/<c-r>//d<cr>gg
" Email (de-)quotation
nmap <leader>q vip:s/^/> /<cr>
vmap <leader>q :s/^/> /<cr>
nmap <leader>Q vip:s/^> //<cr>
vmap <leader>Q :s/^> //<cr>
File Opening and Saving
" Save and restore session
nmap <leader>ss :wa<cr>:mksession! $HOME/.vim/sessions/
nmap <leader>rs :wa<cr>:source $HOME/.vim/sessions/
" Write buffer through sudo
cnoreabbrev w!! w !sudo tee % >/dev/null
" Change the current directory to the directory of the file in buffer
nmap <silent> <leader>cd :cd %:p:h<cr>:pwd<cr>
" Open file located in the same directory as the current one
nmap <leader>e :e <c-r>=expand('%:p:h').'/'<cr>
Grep the word under cursor:
Using the following grepprg options it will search recursively into the current directory excluding and including some specific files.
" Quick Grep
noremap <Leader>g :grep<space><C-r><C-w><CR>:copen<CR><CR><C-W>b
set grepprg=grep\ -nH
\\--include='*.c'
\\--include='*.cpp'
\\--include='*.h'
\\--exclude-dir='.svn'
\\--exclude='*.svn-base'
\\--exclude-dir='OBJ'
\\--exclude='symTbl.c'
\\ $*
\\ -R\ .
It greps the word under cursor, then open the Quickfix Window and move the cursor to the bottow window (which should be the list of grep results)
This is probably one of the shortcut I use the most, and it saves lots of typing !
Moving around quickly between windows
noremap <C-j> <C-W>j
noremap <C-k> <C-W>k
noremap <C-h> <C-W>h
noremap <C-l> <C-W>l
It is quite intuitive and handy to move around when your screen is split horizontally and vertically.
The following command remaps ; to : in command mode, saving you from wasting precious milliseconds holding and releasing the Shift key when typing commands like :wq:
" Remap ";" to ":"
map ; :
noremap ;; ;
If you need to type an actual ;, just press it twice.
Insert mode
" <esc> in normal mode clears highlight
nnoremap <silent> <esc> :noh<cr><esc>
Command Line Editing
" copy an entire word from the line above instead of just one
inoremap <expr> <c-y> matchstr(getline(line('.')-1), '\%' .
\ virtcol('.') . 'v\%(\k\+\\|.\)')
" Insert Directory of current buffer and open completion
cnoremap <expr> <c-k> getcmdline()[getcmdpos()-2] == " " ?
\ expand("%:h") . "/\<c-d>" : "\<c-d>"
let mapleader=","
" omnicompletion : words
inoremap <leader>, <C-x><C-o>
" omnicompletion : filepaths
inoremap <leader>: <C-x><C-f>
" omnicompletion : lines
inoremap <leader>= <C-x><C-l>
" toggle TagList
nnoremap <leader>l :TlistToggle<CR>
" toggle NERDTree
nnoremap <leader>n :NERDTreeToggle<CR>
" I like vertically aligned assignation operators
nnoremap <leader>a :Tabularize<Space>
" with | marking the cursor
" it turns this
" function foo(){|}
" into this
" function foo(){
" |
" }
inoremap <C-Return> <CR><CR><C-o>k<Tab>
" push the current ligne up and down
nnoremap <M-D-Up> ddKp
nnoremap <M-D-Down> ddP
" swap word under the cursor with previous word on the left
" from the Vim wiki
nnoremap <M-D-Left> "_yiw?\w\+\_W\+\%#<CR>:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><C-o><C-l>
" swap word under the cursor with next word on the right
" from the Vim wiki
nnoremap <M-D-Right> "_yiw:s/\(\%#\w\+\)\(\_W\+\)\(\w\+\)/\3\2\1/<CR><C-o>/\w\+\_W\+<CR><C-l>
" and I have lusty-explorers "modes" mapped to:
" "files" <leader>f
" "buffers" <leader>b
" "grep" <leader>g
Useful for navigating up/down long lines that wrap to multiple lines on your display: Alt + ↑ or ↓ arrow keys move through screen lines rather than file lines.
map <A-Down> gj
map <A-Up> gk
imap <A-Up> <ESC>gki
imap <A-Down> <ESC>gji

Resources