Useful VIM Shortcuts
(Last edited: October 29, 2019)
Essentials
Command | Description |
---|---|
ESC:q! |
The Most Important Vim Command™ |
i |
Exit normal mode, enter i mode |
ESC |
Exit i mode, enter normal mode |
arrow keys |
Move around |
0 |
Go to beginning of line |
$ |
Go to end of line |
:w |
Save (write) a file |
:q |
Quit a file (must be saved) |
:wq |
Write and quit a file |
:q! |
Force-quit a file, discarding any changes |
u |
Undo |
x |
Delete the character under the cursor |
dd |
Delete a line and move contents one line above |
D |
Delete a line but don’t move contents one line above |
cc |
Delete the contents of the line and go to i mode |
o |
append a new line below the current line and go to i mode |
O |
append a new line above the current line and go to i mode |
p |
Put (paste) deleted text |
⌃F |
Go forward one screen |
⌃B |
Go backward one screen |
G |
Go to last line |
1G or gg |
Go to first line |
/<string> |
Search for <string> |
Steps
Copy and Paste a Whole Line
Command | Description |
---|---|
yy or Y |
Copy the line |
dd |
Or delete the line |
p or P |
Paste the copied or deleted line after or before the current line respectively |
Detailed
This is a brief summary of the key shortcuts and acts as a reference. It is taken from the contents of the file when running the below command in the terminal:
$ vimtutor
Text Editing - Deletion
x
: Delete the character under the cursor
Text Editing - Insertion
i
: Insert text
Text Editing - Append
a:
Append text
Deletion Commands
d$
: Delete to the end of the line, INCLUDING the last character
de
: Delete to the end of the current word, INCLUDING the last character
db
: Delete to the beginning of the current word, EXCLUDING the first
character
dw
: Delete until the start of the next word, EXCLUDING its first character
Motions
$
: Move the cursor to the end of the line, INCLUDING the last character
e
: Move the cursor to the end of the current word, INCLUDING the last
character
b
: Move the cursor to the beginning of the current word, EXCLUDING the first
character
w
: Move the cursor until the start of the next word, EXCLUDING its first
character
Count For A Motion
N$
: Move the cursor N lines to the end of the line, INCLUDING the last
character
Ne
: Move the cursor N words forward to the end of the current word,
INCLUDING the last character
Nb
: Move the cursor N words forward to the beginning of the current word,
EXCLUDING the first character
Nw
: Move the cursor N words forward until the start of the next word,
EXCLUDING its first character
Count For A Deletion
dN$
: Delete N lines to the end of the line, INCLUDING the last character
dNe
: Delete N words to the end of the current word, INCLUDING the last
character
dNb
: Delete N words to the beginning of the current word, EXCLUDING the
first character
dNw
: Delete N words until the start of the next word, EXCLUDING its first
character
Operating On Lines
dd
: Delete a whole line
dNd
: Delete N whole lines
Undo Command
u
: Undo last command
Nu
: Undo last N commands
U
: Fix a whole line
ctrl-r
: Redo command
Put Command
p
: Put previously deleted text after the cursor
Replace Command
rX
: Replace the character at the cursor with X
Change Operator
ce
: Change until the end of the word. Deletes the word and places us in Insert
mode.
The change operator is used with the same motions as delete (d
).
Cursor Location And File Status
ctrl-g
: Show location in the file and the file status
NG
: Move to N line in the file
G
: Move to the bottom of the file
gg
: Move to the start of the file
Search Command
/
: Followed by a phrase to search for the phrase
n
: Search for the same phrase again
N
: Search for the same phrase again in the opposite direction
?
: Followed by a phrase to search for the phrase in the backward direction
ctrl-o
: Go back where you come from
ctrl-i
: Go forward where you come from
Matching Parenthesis Search
%
: Find a matching ),], or }
Substitute Command
:s/old/new
: Substitute ‘new’ for ‘old’ only for the first occurrence
:s/old/new/g
: Substitute ‘new’ for ‘old’ globally in the line
:#,#:s/old/new
: Substitute ‘new’ for ‘old’, where #,# are the line numbers of
the range of lines where the substitution is to be done
:%s/old/new/g
: Substitute ‘new’ for ‘old’ for every occurrence in the whole
file
:%s/old/new/gc
: Substitute ‘new’ for ‘old’ for every occurrence in the whole
file with a prompt whether to substitute or not
How To Execute An External Command
:!COMMAND
: Followed by an external command to execute that command
Writing Other Files
:w FILENAME
: Save the changes made to the text into a file named FILENAME
Selecting Text To Write
TODO: VIM Checkpoint