vim使用技巧

Category: /knowledge /linux
Tags: linux

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

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 line
    • CTRL-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

  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

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

Useful plugins

讨论

提示

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