vim使用技巧
Category: /knowledge /linuxTags: linux
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
Useful plugins
snipmate ————— in .vimrc
autocmd FileType python set ft=python.django
autocmd FileType html set ft=html.markdown.django_template
不知道什么原因, 我的vim
在编辑时HTML不能正确调用出snippets来, 我不得不再敲一遍
:set ft=html.markdown.django_template
可是之后所有的syntax highlight又都没有了. 大概是filetype中有冲突, 可又找不出原因来.
Do it quick
Exit —— 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
Movement
scroll up & down
- CTRL-b and CTRL-f: one page Back/Forward
- CTRL-u and CTRL-d: half page Up/Down
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
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
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
substitution/replace
:#,#s/old/new/g
Do a search and replace only between two line numbers.
or
: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*$//
把某个字符换成TAB
常见的是CSV中的’,’
%s/,/<TAB>/g
<TAB> 是真实的键, 可能会显示成^I
To display
:set list | nolist
or
/\t
or
hyh
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
encryption
You can add password protection to a file (note that it is rather weak).
vim -x your_file
enter encrption key:
digraph
- to enter a symbol: CTRL-Kxx
- show all digraphs:
:digraphs
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
Show ASCII code
:ascii or ga
to show the encoding of current charactor under the cursor, for
Chinese, show the double-byte value.
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-
-
-