`

Vim find & replace

阅读更多
Vim find & replace (RT)
To find and replace one or more occurrences of a given pattern with a new string, use the s[ubstitute] command in vim.

The format of the s[ubstitute] command is:

代码:
:[rang]s[ubstitute] /{pattern}/{string}/{flags} [count]


This command means that for each line in [rang] replace a match of {pattern} with {string}. The {string} can be a literal string, or something special. When [rang] and [count] are omitted, replace in the current line only.When [count] is given, replace in [count] lines, starting with the last line in [rang]. When [rang] is omitted start in the current line.

There are many {flags} can be used, only a few of them are listed bellow:

[&] This flag must be the first one, Used to keep the flags from the previous substitute command

[c] Confirm each substitution. With this flag, you will be asked what to do:type ‘y’ to substitute the selected match, ‘l’ to substitute the match and then quit. ‘ n ‘ to skip the current match, ‘ a ‘ to substitute all the matches. <Esc> and ‘ q ‘ to quit substituting, CTRL-E to scroll the screen up, and CTRL-Y to scroll the screen down.

[g] Find and Replace all occurrences in the line. Without this argument, replacement occurs only for the first occurrence in each line.

[i] Ignore case for the pattern.

[I] Don’t ignore case for the pattern.

For more information, please refer to

代码:
:help substitute


The usage of [rang] can be find by

代码:
:help cmdline-rang


[$] The last line of the file

[.] The current line

[%] equal to 1, $, the entire file

Examples:

代码:
:%s/foo/bar/g


find each occurance of ‘foo’ and replace it with ‘bar’ without asking for confirmation

代码:
:%s/foo/bar/gc


find each occurance of ‘foo’ and replace it with ‘bar’ asking for confirmation first

代码:
:%s/<foo>/bar/gc


find (match exact word only) and replace each occurance of ‘foo’ with ‘bar’

代码:
:%s/foo/bar/gci


find (case insensitive) and replace each occurance of ‘foo’ with ‘bar’

代码:
:%s/foo/bar/gcI


find (case sensitive) and replace each occurance of ‘foo’ with ‘bar’
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics