Vim is a text editor, accessible from a command-line interface. It’s a great tool for quick edits on a single file, without needing to open a full separate text editor.
There are four modes of operation:
- Command mode — We start in this mode by default and can switch to any mode from here. To switch back to this mode, we can press
Esc.- Command-line mode allows us to enter commands (see below). We enter by pressing
:in command mode.
- Command-line mode allows us to enter commands (see below). We enter by pressing
- Insert mode — Used to edit the contents of the file, accessible by pressing
i. - Visual mode — Used to visually select text and run commands over that section of code, accessible by pressing
v.
Neovim is a popular fork of Vim with a huge amount of extensions and extra feature support.
Commands
Command-line mode
Important commands for exiting:
:wto save the file without exiting.:qto exit the editor.:wqto write the file to disc and exit.:q!to exit the editor and ignore changes made.ZZto save and quit.
Normal mode
"*yyto copy the current line."*yGto copy the entire file.ddto cut the current line.pto paste.xto delete the character after the cursor.uto undo the last operation.Shift+vto select the whole line.
Visual mode
yto copy selected text.dto cut selected text.- To paste the text:
pfor after the cursor.Pfor before the cursor.
Getting around
For full screen navigation:1
- To replace the standard arrow keys:
hgoes left one character.jgoes down.kgoes up.lgoes right.
ggto move the cursor to the first line.#Gto move the cursor to the # line.GGto move the cursor to the last line.ztto move screen such that the cursor’s at the top.zbto move the screen so the cursor’s at the bottom.zzto centre screen on the cursor.
CTRL+fto move the cursor forward a full page.CTRL+bto move the cursor backwards a full page.CTRL+uto move the cursor up a half page.CTRL+dto move the cursor down half page.
Resources
- The Vim Learning Curve is a Myth, by Ben Orenstein
Footnotes
-
From this Reddit comment. ↩