Replace all occurrences of a word with another
- From the command line,
%s/find_word/replacement_word/g
Re-format text blocks to wrap to a fixed character width
- Set the text width in characters
:set textwidth=80 - Select the lines of text you want to re-format
vto use visual selection - Reformat
gq - To reformat a single paragraph without selecting it first
{gq}
Turn on (or off) line numbering
- To turn on line numbering, at the : prompt type
set number - To turn off line numbering, type the command
set nonumber - To set relative line numbers
set relativenumber
Insert a comment character as the first character in a sequence of lines (i.e., comment a block)
- Position the cursor on the first character in the first line to be commented
- Press Ctrl-
Vto enter VISUAL BLOCK mode - Use arrow keys to select the first character in each line until the last line in sequence
- Press Shift-
I(the i key), which will put the editor in INSERT mode - Press #, the character to be inserted at the start of each line, note that it will not yet appear on screen
- Press
Esc(wait a couple of seconds), the # character will appear at the start of each line
Remove comment characters from multiple lines
- Assuming # is the comment character, put your cursor on the first # character, press Ctrl-
V - Use arrow keys to go down until the last commented line, the first character in each line should be highlighted
- Press
xto delete all the selected # characters
Delete white space on the left or beginning of a line
- Position the cursor anywhere on the line to be editted
- Enter the command
:left - Any spaces before the first non-space character will be deleted moving the line to the left
Insert or remove a comment characters
- Move the cursor to the first character of the first line you want to comment
- Press
Ctrl + Vto enter Visual Block mode - Use the Down Arrow key to highlight down to the last line
- Press
Shift + I(uppercase I) to enter block insert mode - Type your comment character (e.g., # for Python/Bash, // for C++/JavaScript)
- Press
Esc - Wait a second—the comment characters will automatically populate across all selected lines
- To uncomment: Press
Ctrl + V, select the comment characters using the arrow keys, pressxto delete them