Modal editing
- Normal(
<ESC>
): for moving around a file and making edits - Insert(
i
): for inserting text - Replace(
R
): for replacing text - Visual: for selecting blocks of text
- plain(
v
): - line(
V
): - block(
<C-v>
):
- plain(
- Command-line(
:
): for running a command
Command-line
This mode has many functionalities, including opening, saving, and closing files, and quitting Vim.
-
:q
quit(close window) -
:w
save('write') -
:wq
save and quit -
:e {filename}
open file for editing -
:ls
show open buffers -
help {topic}
open help-
:help :w
opens help for the:w
command -
:help w
opens help for the w movement
-
Movement
- Basic movement:
hjkl
(left, down, up, right) - Words:
w
(next word),b
(beginning of word),e
(end of word) - Lines:
0
(beginning of line),^
(first non-blank character),$
(end of line) - Screen:
H
(top of screen),M
(middle of screen),L
(bottom of screen) - Scroll:
Ctrl-u
(up),Ctrl-d
(down) - File:
gg
(beginning of file),G
(end of file) - Misc:
%
(corresponding item)(Type % to find a matching ),], or }) - Find:
f{character}
,t{character}
,F{character}
,T{character}
- find/to forward/backward {character} on the current line
-
,
/;
for navigating matches
- Search:
/{regex}
,n
/N
for navigating matches - Type CTRL-O to takes you back to OLDER positions
- Type CTRL-I to takes you back to NEWER positions.
- Type CTRL-W to jump from one window to another.
Edits
For manipulating/deleting text, want to use something more than backspace.
-
s
substitute character(equal tocl
) -
~
flips the case of a character
Modifiers
You can use modifiers to change the meaning of a noun, Some modifiers are i
, which means "inner" or "inside", and a
, which means "around"
-
ci(
change the contents inside the current pair of parentheses -
ci[
change the contents inside the current pair of square backets -
da'
delete a single-quoted string, including the surrounding single quotes
Multiple windows
-
:sp
/:vsp
to split window -
<Ctrl-W-hjkl>
: change window to left/up/down/right
Insert mode
- Type o to open a line BELOW the cursor and start Insert mode.
- Type O to open a line ABOVE the cursor.
- Type i to insert text BEFORE the cursor.
- Type a to insert text AFTER the cursor.
- Type A to insert Text after the end of the line.
- Type I to insert Text BELOW the start of the line.
Search
- Typing / followed by a phrase searches FORWARD for the phrase.
- Typing ? followed by a phrase searches BACKWARD for the phrase.
- Type n to find the NEXT occurrence in the same direction.
- Type N to find the BEFORE occurrence in the same direction.
Undo & Redo
- To undo previous actions, type u
- To undo all the changes on a line, type U
- To undo the undo's, type CTRL-R
Substitute
- To substitute new for the first old in a line type :s/old/new
- To substitute new for all 'old's on a line type :s/old/new/g
- To substitute phrases between two line #'s type :#,#s/old/new/g
- To substitute all occurrences in the file type :%s/old/new/g
- To ask for confirmation each time add 'c' :%s/old/new/gc
Set options
Typing ":set xxx" sets the options 'xxx'. Options are:
- 'ic' 'ignorecase' ignore upper/lower case when searching
- 'is' 'incsearch' show partial matches for a search parase
- 'hls' 'hlsearch' highlight alll matching phrases
- Prepend 'no' to seitch an option off: ":set noic"
Operate file
Common
<operator> [<number>] <motion>
<operator> - is what to do.
- d - is the delete operator
- c - is the change operator
- y - is the yank(copy) operator
<number> - is an optional count to repeat the motion.
<motion> - moves over the test to operate on.
- w - until the start of the next word, EXCLUDING its first character.
- e - to the end of the current word, INCLUDING the last character.
- $ - to the end of the line, INCLUDING the last character.
Special
- Type dd to delete a whole line.
- Type cc to delete the word and places you in Insert mode for the whole line.
Replace
- Type r to replace the character under the cursor.
- Type R to enter Replace mode until <ESC> is pressed.
Delete
- Press x to delete the character under the cursor.
Paste
- Type p to put previously deleted text after the cursor.
Others
- Type :! followed by an external command to execute that command.