Forwarded from http://www.unix.com/unix-dummies-questions-answers/132518-vim-tips-tricks.html
:w
=> Save [:w filename writes the content to the specified file from current file]
:q
=> Exit as long as there have been no changes
:q!
=> Exit and ignore any changes
:wq
=> Save and Exit.
=> Exit, saving changes
ZZ
=> Exit and save changes if any have been made
:10,20w filename
=> writes the line from 10th line to 20th line in given file name
------------------------------------------------------------------------------------------------------------
i
: Insert before cursor
I
: Insert before line
a
: Append after cursor
A
: Append after line
o
: Open a new line after current line
O
: Open a new line before current line
r
: Replace one character
------------------------------------------------------------------------------------------------------------
d^
: Deletes from current cursor position to the beginning of the line.
d$
: Deletes from current cursor position to the end of the line.
dw
: Deletes from current cursor position to the end of the word.
dd
: Deletes three lines from current cursor position downwards.(also :d)
yy
: Yank the current line (also :y)
x
: Delete a character next to the cursor
X : D
elete character to the left of cursor
P
: Paste line before the cursor
p
: Paste line after the cursor
.
: Repeat last edit command
u
: Undo last edit (Ctrl+r redo)
U
: Undo changes to current line
*** Ctrl+p word completion
-----------------------
-----------------------
-----------------------
-----------------------
-----------------------
/pattern
: search for pattern
?pattern
: search backward for pattern
n
:repeat search in same direction
N
: repeat search in opposite direction
:%s/old/new/g
: replace all old with new throughout file
:%s/old/new/gc
: replace all old with new throughout file with confirmations
We can change the substitution delimiter instead of "/" to % or # or @ or !.
Refer the Following Link.
http://www.unix.com/unix-dummies-questions-answers/131585-changing-vi
.html
:/pattern/=
=> Print the line number of the first line that matches pattern.
:g/pattern
=> Finds (moves to) the last occurrence of pattern in the file.
:g/pattern/p
=> Finds and displays all lines in the file containing pattern.
:g!/pattern/nu
=> Finds and displays all lines in the file that don't contain pattern;
also displays the line number for each line found.
:60,124g/pattern/p
=> Finds and displays any lines between lines 60 and 124 containing pattern
-------------------------------------------------------------------------------------------------------------------------------
: pwd
Print the current directory name.
Visual mode.
Shift+v
is to select a line
Ctrl+v
is to select a block of text.
Encryption
:X
其他常用:
1. 插入一列(注釋時常用)
(1) ctrl+v 進入visual block mode
(2) 選中一塊,按住shift+I, 然後鍵入要insert的內容
(3) esc 搞定
2. gg跳到文首,G跳到文末
3. w 跳到下個字首 b跳到一個字首
4. $ 跳到行尾, ^或者0跳到行首
5. :set nu 顯示行號
6. ctrl+f ctrl+b 往前往後翻頁
7. ctrl+d ctrl+u 往下往上翻半頁