Vim Cheatsheet: Basics Link to heading

Let us start with the notation:

C-a = Ctrl-a

Getting Started Link to heading

vim <file>                  Open file.
vim <file1> <file2>         Open file1 and file2
vim -R <file>               Open file in read mode
view <file>                 Open file in read mode
:h[elp] <keyword>           Open help for the keyword
:term                       Open a terminal
:! <cmd>                    Execute shell command <cmd>
<C-\><C-n>                  Go to command mode in the built-in terminal
ESC                         Exit insert mode
:q                          Close document if no modification has been done
:w                          Save the document
:wa                         Save all windows
:wq                         Save and close
:x                          Save and close, equivalent to wq
:q!                         Close and ignore changes

Other fundamental commads are the following

:set spell                  Start the spell checking
:set cc=100                 Creates a vertical line at character 100. 

In the last command, we can change the value 100 to other value. Moreover, cc stands for colorcolumn

  • One line
h                           Left
j                           Down
k                           Up
l                           Right

If we consider the notation nh, where n is a number known as replication factor, the left motion is repeated n times. As an example, we have the equivalence: 4h = hhhh. The same is true for j, k and l.

0                           Move cursor to the beggining of the line
$                           Move cursor to the end of the line
^                           Move cursor to the first non-blank character
  • Text blocks
w                           Move forward one word
W                           Move forward one word (ignore punctuation)
b                           Move backward one word
B                           Move backward one word (ignore punctuation)
e                           Move forward to the end of the word
E                           Move forward to the end of the word (ignore punctuation)
gg                          Move the first line of the document
G                           Move the last line of the document

If we write nG, where n, the cursor goes to line n. The same is true for the other motions. Then, 1G is equivalent to gg.

  • Large movements
H                           Go to the first character on the top
M                           Go to the first character on the middle
L                           Go to the first character on the bottom
C-F                         Scroll forward one page
C-B                         Scroll backward one page
C-D                         Scroll forward half page
C-U                         Scroll backward half page
  • Moving around sentences, paragraphs and sections
(                           Go to the beginning of the sentence
)                           Go to the beginning of the next sentence
{                           Go to the beginning of the paragraph
}                           Go to the beginning of the next paragraph
[[                          Go to the beginning of the section
]]                          Go to the beginning of the next section

Edition Link to heading

  • Inserting text
i                           Insert cursor goes to the beginning of the block cursor
I                           Insert cursor goes to the beginning of the line
0i                          Insert cursor goes to the beginning of the line.
a                           Append cursor goes to the end of the block cursor
A                           Append cursor goes to the end of the line
$a                          Append - the insert cursor goes to the end of the line
o                           Text line after the cursor
O                           Text line before the cursor
  • Changing text
cw                          Change word
c$                          Change text to the end of the line
C                           Change text to the end of the line. Equivalent to c$
c0                          Change text starting from the beggining of the line
cc                          Change the entire line
  • Replacing text
r                           Replace a single character
R                           Replace characters. Overstrike.
s                           Substitute character. Enters in Insert mode
S                           Substitute the whole line. Similar to C
~                           Change case: lowercase to uppercase and vice-versa
  • Delete text
dw                          Delete a word starting from the cursor position
dd                          Delete the line
D                           Delete the line. Equivalent to dd
d0                          Delete from cursor to the beggining of the line
d$                          Delete from the cursor position to the end of the line
x                           Delete a single character under the cursor
X                           Delete a single character in front of the cursor
  • Undoing, repeating and joining
u                           Undo recent modification
C-r                         Redo
U                           Retores the line
.                           Repeat the last command
J                           Join the cursor line with the line below it

When we delete a character, word or line, vim saves the deleted object in a temporary register, so we can recover this deletion as long as we do not delete any other object. We have seen that we undo this deletion with the command u. It also true that we can paste the deleted object in a new place. Let us take a look on these commands.

Copy and Paste Link to heading

yw                          Yank (Copy) word
yy                          Yank the whole line
Y                           Yank the whole line. Equivalent to yy
y$                          Yank to the end of the line
y0                          Yank to the beginning of the line
p                           Paste after the cursor
P                           Paste before the cursor
"+y                         Yank into the system clipboard
"+p                         Paste from the system clipboard
/pattern                    Search pattern forward
?pattern                    Search pattern backward
n                           Repeat search in the same direction
N                           Repeat search in the opposite direction
/                           Repeat search forward
?                           Repeat search backward