Here are the commands you’ll actually use.
The modes
Vim has four modes, and this is the foundation of everything:
- Normal mode: the default mode. You navigate and run commands. You don’t write.
- Insert mode: you type text, like in any other editor. Enter it with
i, exit withEsc. - Visual mode: you select text (with
v). - Command mode: you type commands after
:at the bottom of the screen.
If you’re lost, press Esc to go back to normal mode. Always.
Moving around
In normal mode:
hjkl: left, down, up, right (yes, not the arrow keys, although they also work)w: next wordb: previous word0: start of the line$: end of the linegg: start of the fileG: end of the file:10: go to line 10
Writing text
To switch to insert mode:
i: insert before the cursora: insert after the cursoro: new line belowO: new line above
Esc to return to normal mode.
Copy, cut, paste
In normal mode:
yy: copy the linedd: cut the linep: paste after the cursorP: paste before
In visual mode (with an active selection):
y: copy the selectiond: cut the selection
Search and replace
/pattern: search forward?pattern: search backwardn: next occurrenceN: previous occurrence:%s/old/new/g: replace everywhere in the file:s/old/new/g: replace on the current line
Undo
u: undoCtrl + r: redo
Multiple files
:e file: open a file:sp file: open in a horizontal split:vsp file: open in a vertical splitCtrl + wthenh/j/k/l: switch between windows
Advanced commands
These change your life once you know them:
.: repeat the last action. Probably the most useful command in Vim.ci": clear the content between quotes and switch to insert. Also works withci(,ci{,ci[.da(: delete the parentheses and their content.>G: indent from the current line to the end of the file.=G: re-indent to the end of the file.
Macros
For repetitive tasks:
qa: start recording into register “a”- Do your actions
q: stop recording@a: replay the macro10@a: replay the macro 10 times
.vimrc
The ~/.vimrc file holds your configuration. A reasonable minimum:
set number " line numbers
set autoindent " auto indentation
set tabstop=4 " tab width
set expandtab " spaces instead of tabs
syntax on " syntax highlighting
That’s a starting point. Vim config is a rabbit hole: you can spend hours on it. Start simple. To put Vim through its paces on a short front-end project, try a spinning 3D cube in CSS.
