Vim Commands Cheat Sheet
This cheat sheet provides essential Vim commands to enhance your text editing productivity. Learn shortcuts for common tasks like copying, deleting, replacing, and commenting lines efficiently.
External Resources:
Copying Lines
To duplicate a line you are currently on:
yyp
To copy a block of text:
- Enter visual mode by pressing
v
. - Use direction arrows to highlight the desired text.
- Press
y
to copy the highlighted selection. - Move to the desired insertion point.
- Press
p
to paste.
Deleting Text
To delete the current line:
dd
To delete all text from the cursor to the end of the file:
dG
To delete all text from the cursor to the beginning of the file:
dH
To delete a specified number of lines below the cursor (e.g., 10 lines):
10dd
To delete the first N characters of every line (e.g., first 4 characters):
:%s/^.\{0,4\}//
Example:
>>> class HashTable:
... def __init__(self):
... self.size = 256
... self.slots = [None for i in range(self.size)]
... self.count = 0
After applying the command:
class HashTable:
def __init__(self):
self.size = 256
self.slots = [None for i in range(self.size)]
self.count = 0
Replacing Characters and Text
Normal Mode Function: Find and Replace
Consider the following dataset:
123. 123
233. 123
To replace the period (.
) with a comma (,
) on each line:
:%norm f.C,
Result:
123, 123
233, 123
Commenting Lines
- Enter visual line mode by pressing
Shift + v
. - Move down using arrow keys to select the lines you want to comment.
- Press
Shift + I
to enter insert mode at the beginning of each selected line. - Type the comment character (e.g.,
#
). - Press
Esc
to exit insert mode and apply the comment to all selected lines.
Search and Replace (e.g., replace 'true' with 'false')
:%s/true/false/g
Press Enter to execute the replacement globally on all lines.