博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
vim使用
阅读量:6580 次
发布时间:2019-06-24

本文共 4705 字,大约阅读时间需要 15 分钟。

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

The :substitute command searches for a , 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.

 

Details

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.

转载地址:http://iyino.baihongyu.com/

你可能感兴趣的文章
尝试使用iReport4.7(基于Ubuntu Desktop 12.04 LTS)
查看>>
动态规划:金矿模型
查看>>
子元素应该margin-top为何会影响父元素【转】
查看>>
AJAX 状态值(readyState)与状态码(status)详解
查看>>
BZOJ3668:[NOI2014]起床困难综合症(贪心)
查看>>
LightOJ 1245(Harmonic Number (II))
查看>>
小知识记录
查看>>
css3 animate 和关键帧 @-webkit-keyframes
查看>>
文字链接颜色设置
查看>>
图片转流
查看>>
ubunto应用软件
查看>>
HTML 标签说明
查看>>
锋利的jQuery-2--判断jQuery获取到的对象是否存在$().length
查看>>
linux 查询系统版本命令、查询端口号是否被占用命令
查看>>
java笔记八:IO流之字符流与字符缓冲流
查看>>
Docker 命令收集
查看>>
myeclipse注册码生成器
查看>>
怎样快速学好PHP技术之PHP学习方法总结
查看>>
iOS App间相互跳转漫谈 part2
查看>>
Java CAS 原理剖析
查看>>