vi
, short for vi
sual editor, is a very useful pre-installed standard text editor in most systems. (A joke: vi is not the six editor.) And vim
is a clone of vi
, which is short for v
i im
proved.
Open, Save, and Close a file
1
2
3
4
5
6
7
8
9
10
11
12
13
# open a file
vim <path to file> # open a specific file
vim # open a new file without a name yet
vim -d <file1> <file2> ... <filen> # diff mode
# save changes
:w # write changes to the file
:sav # save the changed file as other file
# close a file
:e! # back to the last saved version of the file
:q! # close without saving changes
:q # close the file, assuming there is no change
Move in a file
- move by character, can be combined with number argument
h
: left one spacej
: down one linek
: up one linel
: right one space
- move in a line
0
: head of line^
: first non-blank character of line$
: end of line<num>
|
: <num>-th column of line
- move by word, can be combined with number argument
w
: forward by a wordW
: forward by a word, ignore punctuationb
: backward by a worde
: forward by a word, at the end of wordE
: forward by a word, at the end of word, ignore punctuation(
: head of current sentence)
: end of current sentence{
: head of current paragraph}
: end of current paragraph[[
: head of current section]]
: end of current section
- move to line
<num>
G
: go to <num>-th lineg
g
: go to top line of file:<num>
: go to <num>-th lineC-g
: display total line number
Screen movement
- scroll screen:
C-f
: scroll full screen forwardC-b
: scroll full screen backwardC-d
: scroll half screen forwardC-u
: scroll half screen backward
- reposition
z
<Enter>
: scroll current line to topz
.
: scroll current line to middle of screenz
-
: scroll current line to bottom
- redraw the screen
C-l
: redraw the screen
- move in screen
H
: move to top of screenM
: move to middle of screenL
: move to bottom of screen<num>
H
: move <num> line below top line of screen<num>
L
: move <num> line above bottom line of screen
Search
/
<pattern>
: search <pattern> forward?
<pattern>
: search <pattern> backwardn
: search next in the same directionN
: search next in the opposite direction
Mark
m
<mark>
: mark current position with <mark>'
<mark>
: move to head of the line which is marked by <mark><Backquote>
<mark>
: move to the character marked by <mark><Backquote>
<Backquote>
: return to previous mark'
'
: return to the head of line of previous mark
Edit
i
: start inserting at current caret positionI
: insert at head of the current linea
: append after the current caret positionA
: append after the end of current lineo
: open a blank line below caretO
: open a blank line above caretc
: change textc
<num>
w|b
: change <num> words, forward or backwardc
0|$
: change to the head or end of the linec
c
: change the whole line
C
: change from current caret position to the end of liner
: replace current caret position character, no need to pressESC
after typingR
: replace inoverstrike mode
s
: substitute current caret position character<num>
s
: substitute <num> characters from current caret position
S
: substitute the entire line<num>
S
: substitute <num> line
~
: change case
Join
J
: join current line with the line below it<num>
J
: join <num> lines
Delete
d
: delete current caret position characterd
<num>
<action>
: delete <num> movement actionsd
d
: delete the entire line
D
: delete to the end of the linex
: delete current caret position character
Paste
p
: paste after current caret position"
<num>
p
: paste from buffer <num>"
<name>
p
: paste from a named buffer
P
: paste before current caret position
Yank
y
: yank the selected texty
<num>
<action>
: yank <num> movement actions"
<name>
y
: yank to a named buffery
y
: yank the entire line
Y
: yank the entire line
Replace
:s/old/new/
: replace first occurrence of pattern old with new, on current line:s/old/new/g
: replace all occurrence of pattern old with new, on current line:<n1>,<n2>s/old/new/[g]
: replace between line <n1> and <n2>, i.e., 1,$ denotes full file:%s/old/new/[g]
: replace for every line in the file:s/old/new/c
: confirmation:g/pattern/s/old/new/g
: limit replacement by pattern, first g denotes for all lines in the file, last g denotes replacement happens globally in the filtered line
Actually, :g
denotes for repeating command.
Repeat and Undo
.
: repeat the last edit commandu
: undo the last edit commandU
: undo all edits on the current line
Fold
To preserve folds between sessions, use :mkview
and :loadview
.
z
A
: toggle state of folds, recursivelyz
a
: toggle state of one foldz
C
: close folds, recursivelyz
c
: close one foldz
O
: open folds, recursivelyz
o
: open one foldz
D
: delete folds, recursivelyz
d
: delete one foldz
E
: eliminate all foldsz
f
: create fold from current line to the line denoted by move action<num>
z
F
: create fold covering <num> lines from current linez
M
: setfoldlevel
to 0z
m
: decrementfoldlevel
z
r
: incrementfoldlevel
z
i
: togglefoldenable
z
N
: setfoldenable
z
n
: resetfoldenable
z
j
: move cursor to start of next foldz
k
: move cursor to end of previous fold
Other
:set spell
:set spelllang=en,da,de,it
:set spellsuggest=5
To show the suggestions, press z-=