vim使用技巧

Category: /knowledge /linux
Tags: linux

Resources

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

  1. Whole lines i_CTRL-X_CTRL-L this mataches the whole line
  2. keywords in the current file i_CTRL-X_CTRL-N
  3. keywords in ‘dictionary’ i_CTRL-X_CTRL-K
  4. keywords in ‘thesaurus’, thesaurus-style i_CTRL-X_CTRL-T
  5. keywords in the current and included files i_CTRL-X_CTRL-I
  6. tags i_CTRL-X_CTRL-]
  7. file names i_CTRL-X_CTRL-F
  8. definitions or macros i_CTRL-X_CTRL-D
  9. Vim command-line i_CTRL-X_CTRL-V
  10. User defined completion i_CTRL-X_CTRL-U
  11. omni completion i_CTRL-X_CTRL-O
  12. Spelling suggestions i_CTRL-X_s
  13. 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 line
    • CTRL-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-

评论

提示

  • 如果看不到评论部分, 请暂时关掉adblock in Firefox/Chrome
  • 本网站使用Javascript实现评论功能, 此处外链对提高您的网站PR没有帮助. (潜台词: 请不要灌水, 谢谢)