vim使用技巧
Category: /knowledge /linuxTags: linux
- Do it fast
- Modes
- Movement
- Search
- Substitude
- Files
- Register
- Macro
- Global command
- Auto-completion
- Settings
- Autocommands
- Convention
- Resources
- Useful plugins
Do it fast
Save and Quit
ZZ (two strokes)
:wq (three strokes)
:wqa (save all files and quit)
Open files with read-only
vi -R
- with vi editor,
:view file
- reload the file:
:e file
or:vi file
Insert/filter
!motion command
: e.g.!10G sort
to sort current line to line 10!!ls
: list all files under current folder and insert them into current position!!date
: insert date/time stamp to current position
Encryption
You can add password protection to a file (note that it is rather weak).
vim -x your_file
enter encrption key:
Modes
Normal Mode
Insert Mode
Delete in Insert mode
These keys also work in vim command mode and bash shell.
<C-h> delete previous char (the same as Backspace)
<C-w> delete previous word
<C-u> delete up to the beginning of the line
Insert-Normal mode
<ESC>
and <C-[>
both enter to normal mode.
Sometimes, we just want to enter a normal command and go back to insert mode immediately.
<C-o>
is good for this purpose. E.g. under insert mode, we can do <C-o>zz
. It refresh the screen, and make the cursor to the middle of the screen, then we can continue the editing without leaving normal mode.
Copy text in insert mode
We can use <C-r>{register}
to paste the context in registers to current place.
E.g. use y
command to copy the text in register 0
, use <C-r>0
to paste it to current position.
<C-r><C-p>{register}
can preseve the indention. (:h i_CTRL-R_CTRL-P)
digraph and non-ASCII
- diagraph, to enter a symbol:
<CTRL-k>xx
, e.g.<C-k>?|, or <C-k>12
- show all digraphs:
:h digraph-table
,:h digraphs-default
or:digraphs
<C-v>{123}
decimal ASCII<C-v>u{1234}
heximal unicode<C-v>{nondigit}
for TAB and other non-print chars,<C-v><TAB>
insert a TAB char, not SPACE.
Show ASCII code
:ascii or ga
to show the encoding of current charactor under the cursor, for
Chinese, show the double-byte value.
Visual Mode
Command Mode
Movement
Scroll up & down
- CTRL-b and CTRL-f: one page Back/Forward
- CTRL-u and CTRL-d: half page Up/Down
Jump between marks
- make a mark, you can use [a-z A-Z]:
ma
- go to the beginning of the line of a mark:
'a
- go to the exact position of a mark:
`a
- display marks:
:marks
Search
Substitude
substitution/replace
:#,#s/old/new/g
Do a search and replace only between two line numbers.
or replace globally
:1,$s/old/new/g
to replace all instances
:% s/old/new/gc
Do a search and replace with prompts.
trim off the blanks at the end of line
:%s/\s*$//
Replace a char with TAB
常见的是CSV中的’,’
%s/,/<TAB>/g
<TAB> 是真实的键, 可能会显示成^I
To display
:set list | nolist
or
/\t
or
hyh
Files
windows/buffers/files
A buffer contains a file content, marks, settings and other stuff.
:next :previous :last :first
:bn :bp :last :first
CTRL-^
==:e #
[count]CTRL-^
==:e #[count]
, switch to [count]th file in the list- control window size:
[count]CTRL-W+
to increase windows by one line[count]CTRL-W-
to decrease windows by one lineCTRL-W=
to make all windows equal size
Register
copy between two vi programs
The trick is to use clipboard register (“*).
- in the source file, mark the text need to copy with
"*y
- in the destination file,
"*p
to paste the text, cool
Macro
Global command
Auto-completion
completion
-
Whole lines i_CTRL-X_CTRL-L this mataches the whole line -
keywords in the current file i_CTRL-X_CTRL-N -
keywords in ‘dictionary’ i_CTRL-X_CTRL-K -
keywords in ‘thesaurus’, thesaurus-style i_CTRL-X_CTRL-T -
keywords in the current and included files i_CTRL-X_CTRL-I -
tags i_CTRL-X_CTRL-] -
file names i_CTRL-X_CTRL-F -
definitions or macros i_CTRL-X_CTRL-D -
Vim command-line i_CTRL-X_CTRL-V -
User defined completion i_CTRL-X_CTRL-U -
omni completion i_CTRL-X_CTRL-O -
Spelling suggestions i_CTRL-X_s -
keywords in ‘complete’ i_CTRL-N
Settings
- for i_CTRL-X_CTRL-K:
set dictibnary=/usr/share/dict/words,file1,file2 ...
- set complete=k/your_file (# k is for key in dictionary)
- CTRL-L: redraw the screen
- CTRL-S: stop the output, CTRL-Q to resume
Autocommands
- autocmd group events file_pattern nested command
- list all autocommands:
:autocmd
- remove autocommand:
:autocmd! [group event pattern nested command]
Convention
- Control Key: CTRL-
-
-
Resources
- vim official site
- Vim Doc
- wikia tips for featured tips
- A Byte of Vim, a wonderful book for beginners
- vim cook book
- Seven habits of effective text editing - read it often, inspiring video
- VIM tips - 15 Years of Vi + 5 years of Vim and still learning
- Quick Reference Card 1
- http://jjinux.blogspot.com/2006/11/vim-more-vim-tidbits.html
- http://www.unb.ca/documentation/UNIX/tips/vim/
- http://www.apmaths.uwo.ca/~xli/vim/vim_tutorial.html
- Dr. Chip’s DrawIt plugin