vim使用

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/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.
时间: 2024-08-05 21:13:47

vim使用的相关文章

vim选中字符复制/剪切/粘贴

问题描述: vim 中选中指定字符,进行复制/剪切/粘贴 问题解决: 进入vim中visual模式,visual模式进入,可以有三种方式: (1)在普通模式(normal)下,直接按键 v  就可以进入默认visual模式,可以使用v+j/k/h/l 进行文本选择 注: 使用normal模式下的  v命令,进入visual模式,v+ j/k/h/l   进行文本选中 对于选中的文本进行如下按键: (1.1)d   ------ 剪切操作 (1.2)y   -------复制操作 (1.3)p  

vim复制,粘贴,剪切文本

vim编辑器 引用文本: ------------------------------------------------------------------------------------------------------------------ 时常自我反省,敏感的思想家 你对于自己及四周的环境能够比一般人控制得更好更彻底. 你讨厌表面化及肤浅的东西:你宁愿独自一人也不愿跟别人闲谈,但你跟朋友的关系却非常 深入,这令你的心境保持和谐安逸. 你不介意长时间独自一人,而且绝少会觉得沉闷.

5.5 进入编辑模式 5.6 vim命令模式 5.7 vim实践

5.5 进入编辑模式 5.6 vim命令模式 5.7 vim实践 扩展 vim的特殊用法 http://www.apelearn.com/bbs/thread-9334-1-1.html vim常用快捷键总结 http://www.apelearn.com/bbs/thread-407-1-1.html vim快速删除一段字符 http://www.apelearn.com/bbs/thread-842-1-1.html vim乱码 http://www.apelearn.com/bbs/thr

Vim快捷键

Vim快捷键 普通模式 [行间跳转] gg 游标移动到到第一行 G 游标移动到最后一行 nG 游标移动到第 n 行 Ctrl + o 快速回到上一次(跳转前)光标所在位置 Shift+zz 普通模式下输入即可保存退出vim [行内跳转] w 到下一个单词的开头 e 到下一个单词的结尾 b 到前一个单词的开头 ge 到前一个单词的结尾 0 或 ^ 光标跳转到行头 $ 光标跳转到行尾 f 字母 向后搜索<字母>并跳转到第一个匹配的位置 F字母 向前搜索<字母>并跳转到第一个匹配的位置

linux基本命令整理(三):进程和vim

linux基本命令整理(三) -----------进程和vim 一.进程 1.查看进程 ps:将某个时间点的程序运行的状况截取下来 a:所有的进程 x:后台进程 u:有效的使用者相关的进程(常用组合aux) -IA:也能观察系统所有的数据 axjf:连同部分的程序树状态 -I:今查看和自己bash相关的程序 top:动态的观察进程的变化 -d:后面接描述,就是整个页面刷新的时间:默认是5秒 -b:以批次的方式执行top -n:与-b搭配使用,意义是需要进行几次top的输出结果 如:top -b

secure CRT设置vim显示颜色

第一步:确定是否安装vim-enhanced.基本上都是会安装好的. 第二步:将Emulation下的Terminal设置为Xterm,然后选中后面的两个复选框. 第三步:将外观下的主题设置为Traditional,字体设置为如下. 第四步:如下进行断开连接. 第五步:如下进行再次连接,登陆即可. 接下来,用vim随便打开一个文件. 如上所示,至此.设置成功.

8. vim编辑器高级应用

1. vim主要模式介绍 命令模式.命令行模式.编辑模式 字符操作:i 当前插入, I行首插入, a当前字符之后插入,A行首插入, ESC退出当前模式 2. vim命令模式 3. vim插入模式 4. vim可视化模式 5. vim开发环境 6. gedit a.exe &

Vim命令合集

来源:Vim命令合集 命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filename 打开vim并创建名为filename的文件 文件命令 打开单个文件 vim file 同时打开多个文件 vim file1 file2 file3 ... 在vim窗口中打开一个新文件 :open file 在新窗口中打开文件 :split file 切换到下一个文件 :bn 切换到上一

vim编辑器详解

一.vim简介 vi: Visual Interface,文本编辑器 文本: ASCII, Unicode 文本编辑种类: 行编辑器: sed 全屏编辑器: nano, vi vim – Vi Improved 其他编辑器: gedit 一个简单的图形编辑器 gvim 一个Vim编辑器的图形版本 二.常见使用方法: vim [OPTION]- FILE- 文件操作: 打开文件: +#: 打开文件后,直接让光标处于第#行的行首 +/PATTERN:打开文件后,直接让光标处于第一个被PATTERN匹

vim编辑器讲解

--vim编辑器讲解(vim(vi的升级版),推荐vim) 打开文件(如果该文件不存在,则创建之后,保存,从内存写到硬盘上,不保存,则删除掉该文件) vim filename vim编辑器有3中模式:命令模式,插入模式,尾行模式. 命令模式:刚进入文件的时候开始的状态.u为撤销键 命令模式进入插入模式:可以通过快捷键 i(当前光标位置插入) I(本行的开头) a(当前光标位置之后) A(当前光标所在最后) o O 插入模式返回命令模式:ESC 插入模式:主要用来编辑文本的. 尾行模式:主要用来,