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?
Shortcut | Command |
---|---|
h, j, k, l | Move 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.
Shortcut | Command |
---|---|
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) |
Shortcut | Command |
---|---|
/ | Provide your search term and press ENTER |
n | MOVE to the NEXT occurrence of the search term |
N | MOVE to the PREVIOUS occurrence |
:%s/{old}/{new}/g | REPLACE all occurrences of old with new in the entire file |
Shortcut | Command |
---|---|
: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.
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.
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 <>