Vim is a modal text editor, an evolved version of the classic vi: available in practically every Unix system, it never asks your hands for the mouse and, once mastered, noticeably speeds up editing code and configs.
Modes#
The whole point of Vim is in modes: the same key means different things depending on which mode you are in.
- Normal — navigation and commands; Vim always starts here
- Insert — typing text (enter:
i/a/o, exit:Esc) - Visual — selecting text (enter:
v/V/Ctrl+v) - Command-line — colon commands (
:w,:q,:%s/…/…/)
Navigation#
| Keys | What they do |
|---|---|
h j k l |
left, down, up, right |
w / b |
next / previous word |
0 / $ |
start / end of a line |
gg / G |
start / end of the file |
{ / } |
previous / next paragraph |
Ctrl+d / Ctrl+u |
half a page down / up |
Editing#
i — insert before the cursor a — insert after the cursor
I — insert at the start of a line A — insert at the end of a line
o — a new line below O — a new line above
x — delete a character r — replace one character
dd — delete a line dw — delete a word
yy — copy (yank) a line yw — copy a word
p — paste after the cursor P — paste before the cursor
u — undo Ctrl+r — redo
ci" — change inside the quotes ci( — inside the parentheses
(the important «operator + object» pattern)
The "operator + text object" combination works with other operators too:
di(— delete the contents of parentheses,daw— delete a word together with the space around it,dd— the delete operator for a whole line.
Visual mode#
Select, then apply an operator — that is how bulk edits work:
v — character-wise selection V — line-wise selection
Ctrl+v — block selection (a column)
y — copy the selection d — cut
> / < — shift the selection right / left
u / U — selection to lower / upper case
The classic block-selection trick: Ctrl+v, walk the cursor down the
column, I — type the text, Esc — the text appears on every line of the
selection.
Search and replace#
/pattern — search forward (n — next match, N — previous)
?pattern — search backward
:s/old/new/ — replace the first match in the line
:s/old/new/g — replace in the current line (all matches)
:%s/old/new/g — replace in the whole file
:%s/old/new/gc — the same, confirming each match
Save and exit#
| Command | What it does |
|---|---|
:w |
save |
:q |
quit |
:wq / ZZ |
save and quit |
:q! |
quit without saving (a frequent guest for beginners) |
:w file.txt |
save to another file |
Opened a file without sudo and it won't save? From inside Vim:
:w !sudo tee %— the file will be rewritten with root rights.
Configuration: ~/.vimrc#
" Line numbering
set number
" Syntax highlighting
syntax on
" Auto-indentation
set autoindent
set smartindent
" Tabs and spaces
set tabstop=4
set shiftwidth=4
set expandtab
" Search: case-insensitive, but case-sensitive if the query has capitals
set ignorecase
set smartcase
" Highlight search matches
set hlsearch
Plugins (vim-plug)#
The plugin manager is a single autoload file:
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
The plugin list goes into ~/.vimrc:
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-fugitive' " Git inside Vim
Plug 'airblade/vim-gitgutter' " Marks for changed lines
Plug 'vim-airline/vim-airline' " Status line
call plug#end()
Installation — from inside the editor after a restart: :PlugInstall.
Neovim is the modern fork with the same editing model; its config lives in
~/.config/nvim/init.vim, and many of the plugins listed work in both editors unchanged.