vi技巧合集

VIM 技巧

match & replace

  • match the whole word(eg: match printf but not snprintf/fprintf)
    You can use < to match the beginning of a word and > to match the end:

    1. %s/<printf>/PRINTF/gc
  • match the current line to last line of file

    1. .,$s/printf/PRINTF/gc
  • match the current line to the next two line of file

    1. .,+2s/printf/PRINTF/gc
  • replace word contain /, eg. printf->//print

    1. :s!printf!//printf!gc

text selection

链接地址
If you want to do the same thing to a collection of lines, like cut, copy, sort, or format, you first need to select the text. Get out of insert mode, hit one of the options below, and then move up or down a few lines. You should see the selected text highlighted

Key description
V selects entire lines
v selects range of text
ctrl-v selects colums
gv reselect block

Word & line completion

链接地址

Key description
ctrl-n next word completion(similar word in current file
ctrl-p previous word completion(similar word in current file
ctrl-x, ctrl-l line completion
ctrl-w erases word (insert mode)
ctrl-u erases line ... or on command line

when we command set path value

  1. :set dictionary=/usr/share/dict/words

then, ctrl-x, ctrl-k, the dictionary completion

markers

链接地址
Use markers to set places you want to hotfoot get back to, or to specify a block of text you want to copy or cut.

key description
ma a mark current position as mark a(can use a-z)
'k move to mark k
d'k delete form current positon to mark k
'a-z same file

reformatting

链接地址
These are useful to reformat text paragraphs or chunks of code (NOTE: This does not python code ...)

key description
V= select text, then reformat with =
= will correct alignment of code
== one line
gq reformat paragraph

Folding

链接地址
Use folds to collapse selected blocks. Useful if you have finished a subroutine and want to save window space, or maybe want to fold allboocks of comments

key description
zo open current scope fold
zc close current scope fold
zO open current scope fold and sub scope fold
zC close current scope fold and sub scope fold

insert text beginning multi-line of same columns

链接地址

  • Use Ctrl+V to select the first column of text in the lines you want to comment.
  • Then hit shift+i and type the text you want to insert.
  • Then hit Esc wait 1 second and the inserted text will appear on every line.




原文地址:https://www.cnblogs.com/xiao13149920yanyan/p/4481447.html