Vim Notes
Vim Notes
Open/Create new File with Vim
$ vim <filename>
- If
<filename>
already existed, then open file with vim - If
<filename>
does not existed yet, then create a new file named<filename>
with vim
- If
How to Quit/Save
- In Normal Mode:
- Save:
:w<Enter>
- Quit without saving:
:q<Enter>
- Quit with saving:
:wq<Enter>
- Save:
How to switch between Normal Mode and Insert Mode
Normal Mode -> Insert Mode
- Insert:
- Enter insert mode before current cursor positon:
i
- Enter insert mode at the start of the current line:
I
- Enter insert mode before current cursor positon:
- Append:
- Enter insert mode after current cursor positon:
a
- Enter insert mode at the end of the current line:
A
- Enter insert mode after current cursor positon:
- Open a new line:
- Enter insert mode with a new line below current line:
o
(Open a new line below) - Enter insert mode with a new line above current line:
O
(Open a new line above)
- Enter insert mode with a new line below current line:
- Go to the previous edit position(and enter insert mode):
gi
- Delete current character/line and switch to insert mode:
s
/S
Insert Mode -> Normal Mode
<ESC>
<Ctrl>+[
<Ctrl>+c
(Could interrupt some plugins)
Command Mode
- In Normal Mode, Enter
:
to enter Command Mode, and Press Enter to confirm a command- Save:
:w
- Quit:
:q
, can also be used to quit splitted windows - Vertical Split:
:vs
- (Horizontal)Split:
:sp
- Replace All:
:% s/<string to be replaced>/<new string we want>/g
- Save:
Visual Mode
- In Normal Mode, Enter
v
to enter Visual Mode, continue selecting with direction keys- Select the whole line:
V
- Select a block:
<Ctrl>+V
- Select the whole line:
Insert Mode
- Delete previous character:
<Ctrl>+h
- Delete previous word:
<Ctrl>+w
- Delete current line:
<Ctrl>+u
Normal Mode
- Undo:
u
Delete
- Delete character:
x
- Delete n characters:
nx
(n is a number) - Delete word:
- Delete word only:
diw
- Delete around word(include the spaces around the word):
daw
ordw
- Delete word only:
- Delete line:
dd
- Delete n line:
ndd
(n is a number) - Delete content until x(e.g. ‘)’):
dt)
- Delete content unitl the end of the line:
d$
- Delete content from the start of the line to current cursor:
d0
Update
- Replace character with ‘x’:
rx
- Delete current character/line and switch to insert mode:
s
/S
- Delete n character/line and switch to insert mode:
ns
/nS
- Change word:
cw
- Change word until x(e.g. ‘)’):
ct)
Copy Paste
- Copy: select with visual mode, then
y
- Copy current line:
yy
- Copy to system clipboard:
"+y
or:set clipboard=unnamed
theny
- Paste:
p
Navigation
Normal Navigation
- Support direction keys
- h(left) j(down) k(up) l(right)
Navigation by words
- word: split by all kinds of tokens (e.g. function is a word)
- WORD: split by spaces (e.g. function(){} is a WORD)
- Move to the start of the next word/WORD:
w
/W
- Move to the end of the next word/WORD:
e
/E
- Move to the start of the previous word/WORD:
b
/B
- Move to the end of the previous word/WORD:
ge
/gE
Navigation to a character in a line
- Move to the next occurance of a
<character>
in a line(char after your cursor):f<character>
- Move to the previous occurance of a
<character>
in a line(char before your cursor):F<character>
- Move to the position just before the next occurance of a
<character>
in a line:t<character>
- Move to the position just after the previous occurance of a
<character>
in a line:T<character>
- Repeating last character search:
;
,,
- e.g.
fd;;
=>fdfdfd
,fd;,
=>fdfdFd
- e.g.
Horizontal Navigation
- Move to the first character of the line:
0
- Move to the first non-space character of the line:
^
- Move to the last character of the line:
$
- Move to the last non-space character of the line:
g_
Vertical Navigation
- Jump entire paragraph downwards:
{
- Jump entire paragraph upwards:
}
- Move down half a page:
<Ctrl>+d
- Move up half a page:
<Ctrl>+u
Page Navigation
- Move to start of the file:
gg
- Move to a specific line:
<line>gg
- Move to the end of the file:
G
- Back to previous position:
<Ctrl+o>
- Go to the head of the screen:
H
- Go to the middle of the screen:
M
- Go to the Lower of the screen:
L
- Page up(upward):
<Ctrl>+b
- Page down(forward):
<Ctrl>+f
- Set current line to the middle of the screen:
zz
Navigation by search
- a
<pattern>
can be a string or regular expression - Search for next occurance of a
<pattern>
:/<pattern>
- Search for previous occurance of a
<pattern>
:?<pattern>
- Press
<Enter>
to move to the first match - Use
n
to move to the next match - Use
N
to move to the previous match - Run last search:
/<Enter>
(forward),?<Enter>
(backward) - Do a search for the word under the cursor:
*
(forward),#
(backward) - Jump to matching
({[]})
in a line:%
Navigation with Counts
<count><command>
: multiply the effect of<command>
for<count>
times- e.g.
2w
: move cursor forward 2 words - e.g.
2/baby
: move to the second occurance ofbaby
- e.g.
Navigation with Semantics
gd
: jump to the definition whatever is under your cursorgf
: jump to a file in an import
All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.
Comment
TwikooLivere