1,查找替换:
查找替换匹配到的部分:%s/pattern/replace/g
查找删除被一个pattern匹配的行:g/pattern/d
2,重复上次的命令: .
3,正则:
行锚位符: ^ $
重复次数: \{m,n} , \{m}
元字符: * . (.*匹配anything)
特殊字符: \s空格 \+重复一至多次
4,游标控制:w e b ge(useful)
5,f char F char 在line里向后 向前 找char ;, 重复最后一个f或者F动作
6,% 跳至对应的括号部分
7,排序,查重: sort ,
g/^\(.*\)$\n\1$/d
8, c (make change)命令和 d 命令一样可以接受 e w $,因为delete a line太常用,所以,dd(dd 而不是dl,因为按两次d更快)被用来执行这个功能。
9,p是put而非paste(随便你怎么理解),p不仅可以把yank的内容粘到一个地方,还可以把前一次d操作删除的东西put到一个地方。
10,文本位置的定位:G(通常按shift+g)去末行;gg去开头;linenum+G去第linenum行,/pattern搜索包含pattern的行,并跳至第一处匹配初,此时按n(next)或者N(previous)
11,还是对vim的查找替换做个总结:(参考http://vimregex.com/)
首先是命令的总揽:
:range s[ubstitute]/pattern/string/cgiI |
For each line in the range replace a match of the pattern with the string where: |
c | Confirm each substitution |
g | Replace all occurrences in the line (without g - only first). |
i | Ignore case for the pattern. |
I | Don't ignore case for the pattern.
|
range的格式:
Specifier | Description |
number | an absolute line number |
. | the current line |
$ | the last line in the file |
% | the whole file. The same as 1,$ |
't | position of mark "t" |
/pattern[/] | the next line where text "pattern" matches. |
?pattern[?] | the previous line where text "pattern" matches |
\/ | the next line where the previously used search pattern matches |
\? | the previous line where the previously used search pattern matches |
\& | the next line where the previously used substitute pattern matches |
然后是pattern相关:
先是锚位符:词锚位符:\<pattern_word\> 将精确匹配pattern_word单词
句锚位符:^whole_line$ 这个大家都比我熟悉
接下来是元字符和转义字符:
# | Matching | # | Matching |
---|
. | any character except new line | | |
\s | whitespace character | \S | non-whitespace character |
\d | digit | \D | non-digit |
\x | hex digit | \X | non-hex digit |
\o | octal digit | \O | non-octal digit |
\h | head of word character (a,b,c...z,A,B,C...Z and _) | \H | non-head of word character |
\p | printable character | \P | like \p, but excluding digits |
\w | word character | \W | non-word character |
\a | alphabetic character | \A | non-alphabetic character |
\l | lowercase character | \L | non-lowercase character |
\u | uppercase character | \U | non-uppercase character |
再接下来是匹配的数量和贪婪性的控制符:
Quantifier | Description |
---|
* | matches 0 or more of the preceding characters, ranges or metacharacters .* matches everything including empty line |
\+ | matches 1 or more of the preceding characters... |
\= | matches 0 or 1 more of the preceding characters... |
\{n,m} | matches from n to m of the preceding characters... |
\{n} | matches exactly n times of the preceding characters... |
\{,m} | matches at most m (from 0 to m) of the preceding characters... |
\{n,} | matches at least n of of the preceding characters... |
where n and m are positive integers (>0) |
Quantifier | Description |
\{-} | matches 0 or more of the preceding atom, as few as possible |
\{-n,m} | matches 1 or more of the preceding characters... |
\{-n,} | matches at lease or more of the preceding characters... |
\{-,m} | matches 1 or more of the preceding characters... |
where n and m are positive integers (>0) |
再来字符范围相关的描述符:【0-9a-zA-Z],这里有个取补集的符号:^
vi也有捕获字符的机制(grouping and backreference):
形式: s:\(word1\)\(word2\):action_use _\0\1..\9:
上图:
# | Meaning | # | Meaning |
& | the whole matched pattern | \L | the following characters are made lowercase |
\0 | the whole matched pattern | \U | the following characters are made uppercase |
\1 | the matched pattern in the first pair of \(\) | \E | end of \U and \L |
\2 | the matched pattern in the second pair of \(\) | \e | end of \U and \L |
... | ... | \r | split line in two at this point |
\9 | the matched pattern in the ninth pair of \(\) | \l | next character made lowercase |
~ | the previous substitute string | \u | next character made uppercase |
接下来是逻辑连接:\| 叫做alternation,表示或连接
最后是匹配和命令连接在一起:
:range g[lobal][!]/pattern/cmd |
Execute the Ex command cmd (default ":p") on the lines within [range] where patternmatches. If pattern is preceded with a ! - only where match does not occur. |