`
kingquake21
  • 浏览: 262116 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

vi在查找和替换时的转义字符的区别

阅读更多

http://vim.wikia.com/wiki/VimTip188

http://vim.wikia.com/wiki/Search_and_replace

 

When searching :

. , * , \ , [ , ] , ^ , and $ are metacharacters.
+ , ? , | , { , } , ( , and ) must be escaped to use their special function.
\/ is / (use backslash + forward slash to search for forward slash)
\t is tab, \s is whitespace
\n is newline, \r is CR (carriage return = Ctrl-M = ^M)
\{#\} is used for repetition. /foo.\{2\} will match foo and the two following characters. The \ is not required on the closing } so /foo.\{2} will do the same thing.
\(foo\) makes a backreference to foo. Parenthesis without escapes are literally matched. Here the \ is required for the closing \) .

When replacing :

\r is newline, \n is a null byte (0x00).
\& is ampersand (& is the text that matches the search pattern).
\1 inserts the text of the first backreference. \2 inserts the second backreference, and so on.

You can use other delimiters with substitute:

:s#http://www.example.com/index.html#http://example.com/#

Save typing by using \zs and \ze to set the start and end of a pattern . For example, instead of:

:s/Copyright 2007 All Rights Reserved/Copyright 2008 All Rights Reserved/

Use:

:s/Copyright \zs2007\ze All Rights Reserved/2008/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics