1. 设置搜索高亮 : set hlsearch (highlight search) , 永久生效 vim ~/.vimrc 加入这一行保存退出就可以。

取消高亮： nohl

2. 搜索替换：

语法格式： :[range]s[ubstitute]/{pattern}/{string}/[flags] [count]

例如：打开 　　vim  /etc/nginx/sites-available/default 这个文件，然后替换 root 成 flyysr

：20,$s/root/flyysr/g 这样 20 行以后出现的 root 字符都会替换成 flyysr range为20,$(从第20行开始到文件末)

s表示替换

pattern为root

string为flyysr

g表示将每行出现的所有的 root 都替换成 flyysr (如果不加g只替换每行出现的第一个root)

--------------------------------------------------------------------------------------------

## Basic search and replace​​Edit​​

The ​​:substitute​​ command searches for a ​​text pattern​​, and replaces it with a text string. There are many options, but these are what you probably want:

​:%s/foo/bar/g​​Find each occurrence of 'foo' (in all lines), and replace it with 'bar'. ​

​:s/foo/bar/g​​Find each occurrence of 'foo' (in the current line only), and replace it with 'bar'. ​

​:%s/foo/bar/gc​​Change each 'foo' to 'bar', but ask for confirmation first. ​

​:%s/\<foo\>/bar/gc​​Change only whole words exactly matching 'foo' to 'bar'; ask for confirmation. ​

​:%s/foo/bar/gci​​Change each 'foo' (case insensitive due to the ​

​i​​ flag) to 'bar'; ask for confirmation.​

​:%s/foo\c/bar/gc​​ is the same because ​​\c​​ makes the search case insensitive.This may be wanted after using ​

​:set noignorecase​​ to make searches case sensitive (the default).​

​:%s/foo/bar/gcI​​Change each 'foo' (case sensitive due to the ​

​I​​ flag) to 'bar'; ask for confirmation.​

​:%s/foo\C/bar/gc​​ is the same because ​​\C​​ makes the search case sensitive.This may be wanted after using ​

​:set ignorecase​​ to make searches case insensitive.

The ​​g​​ flag means global – each occurrence in the line is changed, rather than just the first. This tip assumes the default setting for the ​​'gdefault'​​ and ​​'edcompatible'​​ option (off), which requires that the ​​g​​ flag be included in ​​%s///g​​ to perform a global substitute. Using ​​:set gdefault​​ creates confusion because then ​​%s///​​ is global, whereas ​​%s///g​​ is not (that is, ​​g​​ reverses its meaning).

When using the ​​c​​ flag, you need to confirm for each match what to do. Vim will output something like:​​replace with foobar (y/n/a/q/l/^E/^Y)?​​ (where foobar is the replacement part of the ​​:s/.../.../​​ command. You can type ​​y​​ which means to substitute this match, ​​n​​ to skip this match, ​​a​​ to substitute this and all remaining matches ("all" remaining matches), ​​q​​ to quit the command, ​​l​​ to substitute this match and quit (think of "last"), ​​^E​​to scroll the screen up by holding the Ctrl key and pressing E and ​​^Y​​ to scroll the screen down by holding the Ctrl key and pressing Y. However, the last two choices are only available, if your Vim is a normal, big or huge built or the insert_expand feature was enabled at compile time (look for ​​+insert_expand​​ in the output of ​​:version​​).

Also when using the ​​c​​ flag, Vim will jump to the first match it finds starting from the top of the buffer and prompt you for confirmation to perform replacement on that match. Vim applies the ​​IncSearch​​ highlight group to the matched text to give you a visual cue as to which match it is operating on (set to ​​reverse​​ by default for all three term types as of Vim 7.3). Additionally, if more than one match is found and you have search highlighting enabled with ​​:set hlsearch​​, Vim highlights the remaining matches with the ​​Search​​ highlight group. If you do use search highlighting, you should make sure that these two highlight groups are visually distinct or you won't be able to easily tell which match Vim is prompting you to substitute.

## Details​​Edit​​

Search range:

 ​​:s/foo/bar/g​​ Change each 'foo' to 'bar' in the current line. ​​:%s/foo/bar/g​​ Change each 'foo' to 'bar' in all the lines. ​​:5,12s/foo/bar/g​​ Change each 'foo' to 'bar' for all lines from line 5 to line 12 (inclusive). ​​:'a,'bs/foo/bar/g​​ Change each 'foo' to 'bar' for all lines from mark a to mark b inclusive (see Notebelow). ​​:'<,'>s/foo/bar/g​​ When compiled with ​​+visual​​, change each 'foo' to 'bar' for all lines within a visual selection. Vim automatically appends the visual selection range ('<,'>) for any ex command when you select an area and enter ​​:​​. Also, see Note below. ​​:.,$s/foo/bar/g​​ Change each 'foo' to 'bar' for all lines from the current line (.) to the last line ($) inclusive. ​​:.,+2s/foo/bar/g​​ Change each 'foo' to 'bar' for the current line (.) and the two next lines (+2). ​​:g/^baz/s/foo/bar/g​​ Change each 'foo' to 'bar' in each line starting with 'baz'.

Note: As of Vim 7.3, substitutions applied to a range defined by marks or a visual selection (which uses a special type of marks '< and '>) are not bounded by the column position of the marks by default. Instead, Vim applies the substitution to the entire line on which each mark appears unless the ​​\%V​​ atom is used in the pattern like: ​​:'<,'>s/\%Vfoo/bar/g​​.