Hello Folks, I have been using vim for about 2 years now. And it is my primary editor when it comes to editing text or coding(other than java ;) ). Since I have been exploring vim for 2 years, I have learned some cool tips which vim users can use. These things are small but makes a big difference when you use them. So let’s go through them one by one.
If you want to take a look at my vim-setup you can have a look here on github.
Uppercase and Lowercase conversion
below mappings takes care of converting a selected text into lower or uppercase.
nnoremap g^ gUiW
nnoremap gv guiW
Beautify JSON
For this mapping you have to have python installed on your system. This mapping takes care of beautifying the json. And if the json is not proper it will show an error message. You can chose your own mapping instead of <leader>j
in my case leader is ‘,’
nnoremap <leader>j :%!python -m json.tool<CR>
autocmd BufNewFile,BufRead *.json set ft=javascript
Do not create backup files
A lot of times vim creates some .swap
or some backup file etc. which we really don’t want and finally we have to remove them manually. Bellow settings tells vim that please do not create any backup files.
set nobackup
set nowritebackup
set noswapfile
Show trailing spaces
Below config will show the trailing spaces at the end of the line.
set list listchars=tab:\ \ ,trail:.,extends:>,precedes:<
Change cursor shape in different modes
In vim by default cursor show up as a square in all modes. I use this config to convert the cursor shape to pipe in insert mode.
let &t_SI = "\<Esc>]50;CursorShape=1\x7"
let &t_EI = "\<Esc>]50;CursorShape=0\x7"
Reload vim after making change in ~/.vimrc
The below autogroup will reload vim every time you write the ~/.vimrc
file. If you put this in your ~/.vimrc file, you will never have to do source ~/.vimrc
from the terminal.
augroup VimReload
autocmd!
autocmd BufWritePost $MYVIMRC source $MYVIMRC
augroup END
Selecting text till the last character
If you use vim’s default $
to go to the end of the line it counts the \n(newline)
in it. To avoid that use the below mappings.If you want to select something till end or till start of line use shift + l or
shift + h` respectively.
vnoremap H ^
vnoremap L g_
nnoremap H ^
nnoremap L g_
Use ; instead of :
To go to command mode in vim we use shift + ;
but actually if we see ;
alone is not doing anything, then why not use it to go to command mode. It will help you to navigate to command mode faster and you don’t have to press two keys.
nnoremap ; :
Deleting everything in the file
Generally we use 3 mappings to delete everything in a file, so why not combine them and use just one mapping.
nnoremap <silent>da ggdG
Jump between relative and normal numbering
To jump between relative and normal numbering we go to command mode and type the command :set nu
or :set rnu
, instead we can map it to something simpler the easier to use. I use F2
and F3
for this purpose.
nnoremap <silent><F2> :set rnu!<CR>
nnoremap <silent><F3> :set nu!<CR>