0. Configure your own Vim
cd ~
vim .vimrc
0.1 Key map
noremap a b
nore
means no recursion. remap a
to b
:
map Q :q<CR>
Map Q
to :q
and Enter
.
0.2 Other useful settings
set number
: Show the number of line.
set relativenumber
: Show the relativenumber of line.
set hlsearch
Highlitht the search result.
set ignorecase
: Ingore the capital and non capital charter in search.
set smartcase
: Smartly select the ingorecase.
set scrolloff=5
: Make sure there will always be 5
lines around your cursor.
0.3 Cheat sheet of Vim
1. Normal Mode
1.1 Small edit in normal mode
Core Idea:
-
[<times>]<operation> [times]<motion>
: Execute the operation for<times>
times with the movement. -
<operation><operation(Same as the previous one)>
: Execute the operation to the current line.
x
: Delete the character.
u
:(Undo) (The last change.) ^R
: Redo. (The last change.)
- If you have done a lot of change in Insert mode and exit to normal mode, all those changes are considered as ONE change.
u
will Undo all those changes.
d
+Movement key: Delete all characters between the current cursor and the end position of the cursor. dd
: Delete this line. Well, actually the “delete” is actually “cut”.Use p
to paste them.
y
:+Movement key (Yank) Copy.Copy all characters between the current cursor and the end position of the cursor. yy
: Copy this line.
c
+Movement key: (Change) Delete all characters between the current cursor and the end position of the cursor. And take you into Insert mode. cc
: Delete this line and enter insert mode.
p
: Paste after the cursor.
P
: Paste before the cursor.
.
: Repeat the previous change.
Using register with yank
"ayy
: Copy this line into register a;
"Ayy
: Copy this line and append it into register a;
"ap
: Paste the content in register a into this line.
motion:
- Combine with the movement key introduced in **Moving your cursor **part.
- Combine with the
i
(in) anda
(Around) instruction and the movement key or group:ciw
: Change In Word(forward).cin[
: Change In [].ci"
: Change in “”.ca"
: Change the whole “…”.
1.2 Diffent ways to get into insert mode
i
: (Insert) Get into Insert Mode with the cursor in the current position.
I
: (Insert) Get into Insert Mode with the cursor move to the start of the current line.
a
: (Append) Get into Insert Mode with the cursor in the next position.
- This could be useful when you try to add something at the end of line, because Vim won’t let you to move you cursor to the hidden charator
\n
.
A
: (Append) Get into insert mode with the cursor move to the end of the currnet line.
o
: (Open a new line). Open a new line above the currnet line and get into insert mode.
O
: (Open a new line). Open a new line under the current line and get into insert mode.
1.3 Moving your cursor
1.3.1 Moving between nearby lines
h
: ⬅️ left arrow key.
j
: ⬇️ down arrow key.
k
: ⬆️ up arrow key.
l
: ➡️ right arrow key.
%
: Go to the another part of the parenthesis matching. ( ‘()’ , ‘[]’, ‘{}’ )
1.3.2 Moving in the screen
H
: Move to the head of the screen.
M
: Move to the middle of the screen.
L
: Move to the bottom of the screen.
<number>
+|
: Move to the <number>
th column.
1.3.3 Moving in one line
w
: Move the cursor forward by one word.
b
: Move the cursor backward by one word.
e
: Move to the end of a word.
0
: Move to the beginning of a line.
$
: Move to the end of a line.
^
: (Shift
+6
) Move to the first non-empty character of this line.
f
+?
: Find the first character ?
after current character in this line.(The cursor will be on this character)
F
+?
: Find the first character ?
before current character in this line.(The cursor will be on this character)
t
+?
: Jump to the character before the first character ?
after current character in this line.(The cursor will be in front of this character)
T
+?
: Jump to the character after the first character ?
after current character in this line.(The cursor will be after this character)
1.3.4 Moving in the file
^U
,^D
: Scroll up; Scroll down.
G
: Move to all the way down; gg
: Move to the top.
<numbers> + G
: Move to the <numbers>
line.
^o
: Go back to the previous cursor position.
^i
: Go forward to the previous cursor position.
1.3.5 Search
/
+ <something>
: Search for <something>
after current position.
?
+ <something>
: Search for <something>
before current position.
n
: search the next one forward.
N
: search the next one backward.
2. Insert Mode
Press i
to get into Insert mode from normal mode. Press Esc
to return to normal mode.
UseCTRL
+ [
to return to normal mode as well.
3. Visual Mode
Just like holding the left buttion of yout mouse in other editor.
- Visual normal: Press
v
to enter. To Select texts.
- Visual line: Press
Shift
+V
to enter. - Visual block: Press
^v
to enter.
Then combine with the movement to select the content.
Then you can do anything you can do in normal mode to the content you just selected.
If your operation is more complex than such as d
or c
, press :
and input normal
+<space>
+Command.
- eg:
:'<,'>normal I sometextaddedAtTheBeginningOfEveryLine
4. Command-line Mode
Press :
to enter.
:w
: Write the changes into the file == save.:w
+ path : save to ‘path’.
:q
: Quit(Close the current window).:qa
: Close all windows.:help
: Returns the information of the particular key or command following the:help
.
4.1 Split
:split
: Divide the window into two parts(Upper and lower).
:split:
(Vertical split) :Divide the window into two parts(Left and right).
Change between windows:
^W
+ movement to move between windows.
4.2 Repalce pattern
:s/<old>/<new>
: Replace the first <old>
with <new>
in this line.
:s/<old>/<new>/g
: Replace all the <old>
with <new>
in this line.
Use:<number1>,<number2>s///g
to replace from line <number1>
to <number2>
.
Use:%s///g
to replace in the whole file.
tips:
Use :%s/^/xxx/g
to insert something in the front of every line. (use $
instead to insert at the end of every line. )
5. Macro
q
+<name of register>
: Start recording macro to register<name of register>
. Or finish recording.
Use @
+<name of register>
to execute the macro.
Use@@
to execute the previous macro.2j