R.I.P B. Moolenar
Welcome to The VIM Cheatsheet, traveler
Basic Modes
VIM is a modal editor. That means that it works with different modes.
- NORMAL Mode: Navigate a document
- INSERT Mode: Editing text
- VISUAL Mode: Copying or deleting text
INSERT Mode
- i : Enter INSERT Mode at the beginning of the cursor position
- I : Enter INSERT Mode at the beginning of the cursor line
- a : Enter INSERT Mode at the end of the cursor position
- A : Enter INSERT Mode at the end of the cursor line
- o : Enter INSERT Mode on a new line below the cursor
- O : Enter INSERT Mode on a new line the line above the cursor
- C : ENTER INSERT Mode wiping everything from the cursor to end of line
NORMAL Mode
- Esc : Enters NORMAL Mode
- Ctrl + [ : Enters NORMAL Mode staying on home row
- Ctrl + C : Enters NORMAL Mode staying on home row
Move around
K
^
H < > L
v
J
- w : Forward to the beginning of the next word
- W : Forward to the beginning of the next W.O.R.D
- b : Backward to the beginning of the prev word
- B : Backward to the beginning og the prev W.O.R.D
- e : Forward to the end of the next word
- E : Forward to the end of the next W.O.R.D
- 0 : Move to the beginning of the current line
- ^ : Move to the first non-blank character if the current line
- $ : Move to the end of the current line
- G : Move to the last line of the file
- gg : move to the first line of the file
Multipliers in navigation
[n] + action > repeat the action n times
For instance: 5w will forward 5 words
Find and replace
- /: searchs forward from the cursor position
- //: repeats prev search
- ?: searchs backwards from the cursor position
- ??: repeats prev search
- n: find the next ocurrence
- N: find the prev ocurrence
- *: searchs the next word the cursor is currently on
- #: searchs the prev word the cursor is currently on
- :%s/find/replace/options
g: replaces globally
i: ignores case
c: replaces globally prompting after each replacement
- :noh clears the search results highlights
Tabs
:tabp (tabpreviuos) - Displays the previous tab
:tabn (tabnext) - Displays the next tab
:tabf (tabfirst) - Displays the first tab
:tabl (tablast) - Displays the last tab
:tabmove 0 - Moves the tab to the given position (0 based)
Files
:w (write) file.txt - Writes a file
:e (edit) file.txt - Opens a file
:q (quit) - quits nvim
:q! (quit) - quits nvim without saving changes
:wq/:x (write and quit) - writes the changes and exits nvim
VISUAL Mode
VISUAL mode allows to visually select characters
v = Enter VISUAL Mode
- Move around the same way like in NORMAL Mode to select text
- y = Copies the selection
- p = Pastes the selection
V = Enter VISUAL LINE Mode
ctrl+v = Enter VISUAL BLOCK Mode
Modify an entire column at once
Imagine that we are given the task to add a preffix to a list of phone numbers, to do so follow these steps:
1 - Enter VISUAL BLOCK Mode
2 - Select the first character for each phone number
3 - Enter INSERT Mode from the beginning of the selection (I) and add the preffix
4 - Hit Esc
900-543
432-098
123-765
Result:
+44 900-543
+44 432-098
+44 123-765
More Commands
:cd - Shows the current directory we are in
:put=range(1,10) - writes a range from 1 to 10
:for (example)
:for i in range(0,255) | put='192.168.0.'.i | endfor
:g/^/m0 - Flip the order of the document rows, last row becomes row 1 and so on
:registers - shows the lists of clipboards (registers) and its contents, registers can also store macros
:oldfiles - shows files and its routes that were recently opened
:browse oldfile - shows files and its routes that were recently opened and allows to open one of the by index
:Sexplore - (:q to close) opens file explorer in a horizontal window
:Vexplore - (:q to close) opens file explorer in a vertical window
:Texplore - (:q to close) opens file explorer in a new tab
Macros
Macros are like a mini scripts, they allow to store a sequence of actions in order to play them back automatically.
Macros are stored in the registers from 'a' to 'z' (26 in total)
Use 'q' to start recording macros. The same key stops macro recording
Use @ followed by the register (a-z) to play the macro
Macros can be multiplied by doing 4 @a, for example...
Marks
Marks allow to save positions of the file in the register (a-z, A-Z) to jump back to them faster.
ma - Sets a mark on the current cursor position (register 'a')
'a - jumps to the line of that mark
`a - jumps to the line and column of the mark
:delmarks a - deletes the mark
Capital letter marks
These marks will remain through open tabs. Using the register 'a' to set a mark will set it on the tab locally.
But using the register 'A' will set it for all across the tabs. That means that if I am in another tab and
call the mark (which was set with capital 'A' in the other tab) vim will forward me to the tab where the mark
was set.