ITeye博客

Vim find & replace

lingceng · 2013-04-10 17:59 · tool · reprint · 开源软件 · Markdown

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

The format of the s command is:

代码: :s /{pattern}/{string}/{flags}

This command means that for each line in replace a match of {pattern} with {string}. The {string} can be a literal string, or something special. When and are omitted, replace in the current line only.When is given, replace in lines, starting with the last line in . When 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

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. and ‘ q ‘ to quit substituting, CTRL-E to scroll the screen up, and CTRL-Y to scroll the screen down.

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

  • Ignore case for the pattern.

  • Don’t ignore case for the pattern.

For more information, please refer to

代码: :help substitute

The usage of 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//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’

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。