2024-05-13

Most productive shortcuts for VIM.


Vi(m) or neovim are amazing editors when it's about productivity. To use them efficiently a solid knowledge about the most useful shortcuts is required. The number of shortcuts could be overhelming even for an experienced user.
The below mentioned list is providing my favourite shortcuts based on the stackoverflow article What is your most productive shortcut with Vim?

Navigation

ShortcutCommand
h, j, k, lMove LEFT, DOWN, UP, and RIGHT, respectively
w, b       Move to the next/previous word
0, $       Move to the beginning/end of the line
gg, G      Go to the start/end of the file
f{char}    Jump to first occurrence of char at current line (use , or ; to jump to next or previous char)

Install vim-sneak plugin and use the following minimal .vimrc config to move even more efficient:

let g:sneak#label = 1
nmap f <Plug>Sneak_s
nmap F <Plug>Sneak_S

The above settings will map f and F keys to search forward and search backwards respectively using the sneak label mode. Now you can press f and im, to highlight all occurrences of im with a given label. Press the character in the label and the cursor will jump to that location.

Editing

ShortcutCommand
i          INSERT at cursor position
A          APPEND at end of line
dd         DELETE the current line
yy         YANK (COPY) the current line
p          PASTE the yanked text
u          UNDO the last change
CTRL + r   REDO
ci"        CHANGE text between ".."
ci(        CHANGE text between (..)
ci<        CHANGE text between <..> (needs set matchpairs+=<:> in vimrc)

Searching and replacing

ShortcutCommand
/Provide your search term and press ENTER
nMOVE to the NEXT occurrence of the search term
NMOVE to the PREVIOUS occurrence
:%s/{old}/{new}/gREPLACE all occurrences of old with new in the entire file

Working with multiple files

ShortcutCommand
:e {filename}OPEN a new FILE
:bnext       SWITCH to next BUFFER
:bprev       SWITCH to previous BUFFER
:bd          CLOSE the current BUFFER

Install fzf plugin using vim-plug as follows:

Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
...
" Remap keys for fzf buffers
nmap ; :Buffers!<CR>
nmap ' :Files!<CR>
nnoremap <Tab> :bnext<CR>
nnoremap <S-Tab> :bprevious<CR>

Now you can access your buffers via ; and search for files via '. Switching between buffers can be done using tab-key.

Visual Mode

To select the next matching parenthesis, use

v%    if the cursor is on the starting/ending parenthesis
vib   if the cursor is inside the parenthesis block

To select text between quotes, use

vi"   for double quotes
vi'   for single quotes

To select a curly brace block, use

viB
vi{

To select the entire file, type

ggVG

where gg jumps to beginning of page, V enters the visual mode and G jumps to the end of the document.

Customization

set number            " Display **line numbers**
set tabstop=4         " Set **tab width** to 4 spaces
set autoindent        " Automatically indent new lines
set expandtab         " Convert tabs to spaces.
set shiftwidth=4      " Set indentation level to 4 spaces
set matchpairs+=<:>   " Match pairs of <>