Notes on playing with VIM
Three steps to learn how to use VIM efficiently:
i. do never touch your mouse
ii. memorize the key mapping in your mind and use them often
iii. just forget about the key mapping
Part I: Basic Cursor Movement
-------------------------------------------------------------------------------------
k up
h l => left right
j down
-------------------------------------------------------------------------------------
w => move the cursor to the first letter of the next word
W=> same as "w", but taking special signs together with alphabets to be a part of word.
e => move the cursor to the last letter of the current word
E => same as "e", but taking special signs together with alphabets to be a part of word.
b => move the cursor to the first letter of the previous word
B => same as "b", but taking special signs together with alphabets to be a part of word.
ps. "special signs" here exclude "_" that is often used in function and variable naming
pps. another way to understand the function of "W", "E" and "B":
only when reaching "space", will it be recognized as the separation of two words
-------------------------------------------------------------------------------------
^ =>move the cursor to the first non-blank of the current line
0 =>move the cursor to the first position of the current line
$ =>move the cursor to the last position of the current line
-------------------------------------------------------------------------------------
gg =>move the cursor to the beginning of the file
G =>move the cursor to the end of the file
ps. (x)gg and (x)G will move the cursor to line (x)
-------------------------------------------------------------------------------------
f(x) =>find the first character of (x) after the cursor
F(x) =>find the first character of (x) before the cursor
Notice: the parenthesis () is only a gesture used for explaining down below. No need to type in VIM.
-------------------------------------------------------------------------------------
Ctrl + e => roll down for one line
Ctrl + y => roll up for one line
Ctrl + d => roll down for half a screen
Ctrl + u => roll up for half a screen
Ctrl + f => roll down for a whole screen
Ctrl + b => roll up for a whole screen
-------------------------------------------------------------------------------------
H => move the cursor to the first line of the current screen
M=> move the cursor to the middle line of the current screen
L => move the cursor to the last line of the current screen
-------------------------------------------------------------------------------------
{ => move the cursor to the previous blank line
} => move the cursor to the next blank line
( => move the cursor to the previous blank line or the first line after the previous blank line
) => move the cursor to the next blank line or the first line after the next blank line
ps. a blank line is a complete empty line, with even no "space" inside
pps. these keys can be used for paragraph navigation
-------------------------------------------------------------------------------------
# => search for the word under cursor upwards
* => search for the word under cursor downwards
/(x) => search the word (x) from the cursor to the end of file
?(x) => search the word (x) from the cursor to the top of file
n => find the next one match
N => find the previous one match
-------------------------------------------------------------------------------------
% => match brackets
-------------------------------------------------------------------------------------
- => move the cursor to the first character of the previous line
+ => move the cursor to the first character of the next line
-------------------------------------------------------------------------------------
m(x) => mark current character with an alphabet (x) as marking
`(x) => move to marking (x)
ps. `. => move to the lastest edit
-------------------------------------------------------------------------------------
Part II: File Editting
-------------------------------------------------------------------------------------
y(x) => copy from the cursor to the position (x)
ps. yy or Y => copy the current line
p => paste after the cursor
ps. Shift + p=>paste at the beginning of the current line
:(x),(y) co (z) => copy from line (x) to line (y), and paste after the line (z)
Notice: the parenthesis () is only a gesture used for explaining down below. No need to type in VIM.
-------------------------------------------------------------------------------------
x => cut the current character under cursor
X => cut the character before cursor (i.e. backspace)
s => cut the current character under cursor and then enter insert mode
S => cut the current line and enter insert mode (same function as C)
d(x) => cut from the cursor to the position (x)
ps. dd => cut the current line
D => cut from the cursor to the end of the current line
c(x) => cut from the cursor to the position (x) and enter insert mode
ps. cc => cut the current line and enter insert mode
C => cut the current line and enter insert mode (same function as S)
-------------------------------------------------------------------------------------
o => start a new line after the current line
O=> start a new line before the current line
J => combine the current line with the next line
-------------------------------------------------------------------------------------
. => repeat the previous file edit action (exclude "undo" action or other command action)
-------------------------------------------------------------------------------------
r(x) => replace current character with character (x)
R => enter REPLACE mode, allowing replacing the character one by one (hit "Esc" to exit)
-------------------------------------------------------------------------------------
~ => character case switch (lower case to upper case, and backwards)
-------------------------------------------------------------------------------------
:s/(x1)/(x2) => swap the next one of the word (x1) with the word (x2) in the current line
:%s/(x1)/(x2) => swap the first word (x1) with the word (x2) in every line
:s/(x1)/(x2)/g => swap all the words (x1) with the word (x2) in the current line
:%s/(x1)/(x2)/g => swap all the words (x1) with the word (x2) in every line
:(n1),(n2) s/(x1)/(x2)/g => swap all the words (x1) with the word (x2) from line (n1) to line (n2)
:g/^\s*$/d => delete all blank lines (^ is start of line; \s* is zero or more whitespace characters; $ is end of line)
-------------------------------------------------------------------------------------
"(x) => register to the address (x) of the registry
usage: "(x) can be followed by many actions like y(x) and p, etc.
ps. use :reg to browse the registry
-------------------------------------------------------------------------------------
=(x) => formalize all lines from current line to the line of position (x)
ps. use == to formalize the current line
-------------------------------------------------------------------------------------
To be continued...
Useful Combination Commands
Multiple Window Operations
-------------------------------------------------------------------------------------
:sp (x) => split the current file into two windows, if (x) is neglected, otherwise, create a new file named (x) in the new window
:new (x) => split the window into two and create a new file named (x) in the new window
:sf (x) => split the window into two and open one existed file named (x) in the new window
:vsp (x) => same as :sp (x), but spliting the window into left and right parts
-------------------------------------------------------------------------------------
:close => close the current window
:only => close all the windows except the current one
ps. :close and :only just close the window(s), the file are still in the buffer after closed.
Only quit commands like :q! or :wq can really close the current file.
-------------------------------------------------------------------------------------
Ctrl + w + j/k => switch to window above (+k) or window below (+j)
Ctrl + w + h/l => switch to window on the left (+h) or window on the right (+l)
-------------------------------------------------------------------------------------
:res(ize) (x) => resize the current window into the space of (x) lines
:res(ize)+(x) => enlarge the space of the current window for (x) lines
:res(ize)-(x) => narrow the space of the current window for (x) lines
ps. for window width resize operation, add vert(ical) to the front of the commands above
-------------------------------------------------------------------------------------
Visual Mode Commands
General Commands
-------------------------------------------------------------------------------------
u => undo
Ctrl + r => redo
-------------------------------------------------------------------------------------
Ctrl + Shift + + => increase font size
Ctrl + Shift + - => decrease font size
-------------------------------------------------------------------------------------
:f (x) => rename the file as (x)
-------------------------------------------------------------------------------------
Reference


浙公网安备 33010602011771号