All the below shortcuts are executed in COMMAND mode, unless otherwise specified.
:! ...
Executes an external command. Example:
:!gcc % Invokes gcc on the current file
:<range>!...
Pipes the text that's in the specified range to an external command and reads back the result into the fileExamples:
:1,6!sort
:%!sort Here, the % means 1, $ (from line 1 till the end of file)
:%!xxd Turns the file into a hex dump, with the linux xxd command
:!xxd -r To reverse back to the text
:%!nl -ba or :%!cat -n Number all lines
:r <filename>
Reads an external file and inserts it at the current cursor position
Example:
:r main.c Inserts the contents of main.c in the next line
:r!
Reads in the results of an external command and inserts it in the next lineExample:
:r! ls -l
:.!
Like :r! but inserts the text in the current line
Example:
:.! ls -l
%
Goes to the matching bracket under the cursorExample:
<action>i...
works on the enclosing text, specificied by the encloserExamples:
ci{ Change the text between the { }
vi' Visually select the text between '
vi" Visually select the text between "
diw Delete the current word
CTRL + v
Block-select modeCTRL + a
Increments the number under the cursorCTRL + x
Decrements the number under the cursor'.
Jumps back to the last edited line`.
Jumps back to the last edited wordgi
Like `. but goes into INSERT modega
Displays the ASCII, hex and octal value of the character under the cursor~
Changes the case of the letter under the cursorCTRL + r (in INSERT mode)
Examples:
" | Unnamed register, containing the text of the last delete or yank |
% | Current filename |
# | Alternate file name |
* | Clipboard contents (X11: primary selection) |
+ | Clipboard contents |
/ | Last search pattern |
: | Last command-line |
. | Last inserted text |
- | Last small (less than a line) delete |
=8*5 | Insert 40 into text (mini-calculator) |
(I have written a whole post about CTRL + r)
:Explorer
Opens a built in file explorerAlthough vim's inbuilt file explorer suffices, I suggest you use NERDTree for a better file explorer.
:earlier 20m
Reverts the document back to how it was 20 minutes ago:later 15m
The reversal of :earlierzz
Scrolls the screen to make the current line in the middle of the page:changes
View the changelistg; and g,
Move backwards and forwards through the changelistge
Moves the the previous word end:perldo and :rubydo
Execute a perl or a ruby one liner on a range of text. If you do not specify a range, the command will execute on all the lines. In the one liner, $_ refers to the text of the line
Examples:
:perldo $_ = join ' ', reverse split Reverse the words of all the lines
:2,4 rubydo $_ += ";" Inserts a semicolon at the end of lines 2 to 4 (This example can also be achieved with :2,4 s/$/; )
q:
View command historygf
Tries to identify the word under the cursor as a file name and open itCTRL + z
Switch back to the shell. Type in fg to return back to vim:sh
Go to sub shell. CTRL + d or type exit to go back to vimggg?G
ROT13 the current file*
Finds the next occurrence of the word under the cursor#
Finds the previous occurrence of the word under the cursor:set cul
Highlights the line containing the cursor:set nu
Turns on line numbers in the left margins:set autochdir
Set the working directory to the location of the current file:set so=3
Scrolls the text so that (when possible) there are always at least three lines visible above the cursor, and three lines visible below the cursorExamples:
:set so=999 Keeps the cursor in the middle line, whenever possible
:set showcmd
Shows the command keystrokes as they are being inputted, at the bottom right of the screengg=G
Corrects indentation for the whole fileguu
Lowercase the entire linegUU
Uppercase the entire linexp
Transpose two characters (switch their positions). This is a simple cut (x) and paste (p) after the cursor operation.CTRL + v (INSERT mode)
Enter a character from its hexadecimal value
CTRL + y (INSERT mode)
Inserts the character above at the current cursor position
CTRL + w (INSERT mode)
Deletes the previous word
gk
Go up one visible linegj
Go down one visible line:tab help ...
Open help in a new pageExamples:
:tab help shiftwidth Opens the help for shiftwidth in a new tab
:qa
Close all windows and tabs (:qa! to force it)