vim使用

本文介绍如何使用Vim进行高效搜索和替换操作,包括设置搜索高亮、取消高亮及不同范围内的替换方法。通过实际例子展示如何进行精确匹配、全局替换及交互确认等高级替换功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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)

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

举一反三:

 

http://vim.wikia.com/wiki/Search_and_replace

Basic search and replaceEdit

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"), ^Eto 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.

 

DetailsEdit

Search range:

:s/foo/bar/gChange each 'foo' to 'bar' in the current line.
:%s/foo/bar/gChange each 'foo' to 'bar' in all the lines.
:5,12s/foo/bar/gChange each 'foo' to 'bar' for all lines from line 5 to line 12 (inclusive).
:'a,'bs/foo/bar/gChange each 'foo' to 'bar' for all lines from mark a to mark b inclusive (see Notebelow).
:'<,'>s/foo/bar/gWhen 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/gChange each 'foo' to 'bar' for all lines from the current line (.) to the last line ($) inclusive.
:.,+2s/foo/bar/gChange each 'foo' to 'bar' for the current line (.) and the two next lines (+2).
:g/^baz/s/foo/bar/gChange 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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值