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/