vim查找与替换

来源:互联网 发布:svm推荐算法 编辑:程序博客网 时间:2024/05/01 21:24

vim提供了:s(substitute)命令进行替换和查找,基本格式如下:

:{作用范围}s{分隔符}{目标}{分隔符}{替换}{分隔符}{替换标志}

作用范围

  • %:全文
  • n,m:第n行到第m行
  • .,$:当前行到最后一行
  • .,+n:当前行到其后n行
  • '<,>':visual模式下选择区域后输入:得到选区

分隔符

常用为/作为分隔符,可以使用其他字符,但同一命令需要保持一致。

替换标志

  • [空]:仅从光标开始替换第一次出现的,仅替换一次
  • g:全局替换,所有出现的都替换
  • i:忽略大小写
  • I:不忽略大小写
  • c:全局替换时进行确认,按下y表示替换,n表示不替换,a表示替换所有,q表示退出查找模式, l表示替换当前位置并退出。^E与^Y是光标移动快捷键

查找

  • ., *, \, [, ^, and $: 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 (space or tab)
  • \n is newline, \r is CR (carriage return = Ctrl-M = ^M)
  • After an opening [, everything until the next closing ] specifies a /collection
  • Character ranges can be represented with a -; for example a letter a, b, c, or the number 1 can be matched with [1a-c]. Negate the collection with [^ instead of [; for example [^1a-c] matches any character except a, b, c, or 1.
  • \{#\} is used for repetition
  • /foo.\{2\} will match foo and the two following characters
  • \ 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 \)

替换

  • \r is newline, \n is a null byte (0x00).
  • \& is ampersand (& is the text that matches the search pattern).
  • \0 inserts the text matched by the entire pattern
  • \1 inserts the text of the first backreference. \2 inserts the second backreference, and so on
  • \zs and \ze to set the start and end of a pattern

示例

  • :%s//bar/g
    Replace each match of the last search pattern with ‘bar’.
    For example, you might first place the cursor on the word foo then press * to search for that word.
    The above substitute would then change all words exactly matching ‘foo’ to ‘bar’.
  • :%s/foo/<c-r><c-w>/g
    Replace each occurrence of ‘foo’ with the word under the cursor.
    <c-r><c-w> means that you press Ctrl-R then Ctrl-W.
    The word under the cursor will be inserted as though you typed it.
  • :%s/foo/<c-r><c-a>/g
    Replace each occurrence of ‘foo’ with the WORD under the cursor (delimited by whitespace).
    <c-r><c-a> means that you press Ctrl-R then Ctrl-A.
    The WORD under the cursor will be inserted as though you typed it.
  • :%s/foo/<c-r>a/g
    Replace each occurrence of ‘foo’ with the contents of register ‘a’.
    <c-r>a means that you press Ctrl-R then a.
    The contents of register ‘a’ will be inserted as though you typed it.
  • :%s/foo/<c-r>0/g
    Same as above, using register 0 which contains the text from the most recent yank command. Examples of yank (copy) commands are yi( which copies the text inside parentheses around the cursor, and y$ which copies the text from the cursor to the end of the line. After a yank command which did not specify a destination register, the copied text can be entered by pressing Ctrl-R then 0.
  • :%s/foo/\=@a/g
    Replace each occurrence of ‘foo’ with the contents of register ‘a’.
    \=@a is a reference to register ‘a’.
    The contents of register ‘a’ is not shown in the command. This is useful if the register contains many lines of text.
  • :%s//<c-r>//g
    Replace each match of the last search pattern with the / register (the last search pattern).
    After pressing Ctrl-R then / to insert the last search pattern (and before pressing Enter to perform the command), you could edit the text to make any required change.
  • :%s/<c-r>*/bar/g
    Replace all occurrences of the text in the system clipboard (in the * register) with ‘bar’ (see next example if multiline).
    On some systems, selecting text (in Vim or another application) is all that is required to place that text in the * register.
  • :%s/<c-r>a/bar/g
    Replace all occurrences of the text in register ‘a’ with ‘bar’.
    <c-r>a means that you press Ctrl-R then a. The contents of register ‘a’ will be inserted as though you typed it.
    Any newlines in register ‘a’ are inserted as^Mand are not found.
    The search works if each ^M is manually replaced with ‘\n’ (two characters: backslash, ‘n’).
    This replacement can be performed while you type the command:
  • :%s/<c-r>=substitute(@a,"\n",'\\n','g')<CR>/bar/g
    The “\n” (double quotes) represents the single character newline; the ‘\n’ (single quotes) represents two backslashes followed by ‘n’.
    The substitute() function is evaluated by the <c-r>= (Ctrl-R =) expression register; it replaces each newline with a single backslash followed by ‘n’.
    The <CR> indicates that you press Enter to finish the = expression.
  • :%s/<c-r>0/bar/g
    Same as above, using register 0 which contains the text from the most recent yank command.

  • :%s/foo/bar/
    On each line, replace the first occurrence of “foo” with “bar”.

  • :%s/.*\zsfoo/bar/
    On each line, replace the last occurrence of “foo” with “bar”.
  • :%s/\<foo\>//g
    On each line, delete all occurrences of the whole word “foo”.
  • :%s/\<foo\>.*//
    On each line, delete the whole word “foo” and all following text (to end of line).
  • :%s/\<foo\>.\{5}//
    On each line, delete the first occurrence of the whole word “foo” and the following five characters.
  • :%s/\<foo\>\zs.*//
    On each line, delete all text following the whole word “foo” (to end of line).
  • :%s/.*\<foo\>//
    On each line, delete the whole word “foo” and all preceding text (from beginning of line).
  • :%s/.*\ze\<foo\>//
    On each line, delete all the text preceding the whole word “foo” (from beginning of line).
  • :%s/.*\(\<foo\>\).*/\1/
    On each line, delete all the text preceding and following the whole word “foo”.
  • :%s/\<foo\(bar\)\@!/toto/g
    On each line, replace each occurrence of “foo” (which starts a word and is not followed by “bar”) by “toto”.
  • :s/^\(\w\)/\u\1/
    If the first character at the beginning of the current line only is lowercase, switch it to uppercase using \u (see switching case of characters).
  • :%s/\(.*\n\)\{5\}/&\r/
    Insert a blank line every 5 lines.
    The pattern searches for \(.*\n\) (any line including its line ending) repeated five times (\{5\}).
    The replacement is & (the text that was found), followed by \r (newline).
  • :%s/\<foo\(\a*\)\>/\=len(add(list, submatch(1)))?submatch(0):submatch(0)/g
    Get a list of search results. (the list must exist)
    Sets the modified flag, because of the replacement, but the content is unchanged.
    Note: With a recent enough Vim (version 7.3.627 or higher), you can simplify this to:
  • :%s/\<foo\(\a*\)\>/\=add(list, submatch(1))/gn
    This has the advantage, that the buffer won’t be marked modified and no extra undo state is created. The expression in the replacement part is executed in the sandbox and not allowed to modify the buffer