Vim find & replace (RT)
To find and replace one or more occurrences of a given pattern with a new string, use the s[ubstitute] command in vim.
The format of the s[ubstitute] command is:
代码:
:[rang]s[ubstitute] /{pattern}/{string}/{flags} [count]
This command means that for each line in [rang] replace a match of {pattern} with {string}. The {string} can be a literal string, or something special. When [rang] and [count] are omitted, replace in the current line only.When [count] is given, replace in [count] lines, starting with the last line in [rang]. When [rang] 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
[c] 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. <Esc> and ‘ q ‘ to quit substituting, CTRL-E to scroll the screen up, and CTRL-Y to scroll the screen down.
[g] Find and Replace all occurrences in the line. Without this argument, replacement occurs only for the first occurrence in each line.
[i] Ignore case for the pattern.
[I] Don’t ignore case for the pattern.
For more information, please refer to
代码:
:help substitute
The usage of [rang] 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/<foo>/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’
分享到:
相关推荐
vim-fnr 在 Vim 中使用实时预览进行 Find-N-Replace(实验性)安装使用您最喜欢的插件管理器。 VIM-FNR需要 。 使用 : Plug ' junegunn/vim-pseudocl 'Plug ' junegunn/vim-fnr '用法正常模式<Leader>r<Movement> -...
- `%s/[find]/[replace]/g`:全局替换,将找到的所有[find]替换为[replace]。 在实际操作中,可以通过以下方式打开文件: - `vim File.cfg`:创建或打开名为File.cfg的文件。 - `vim +6 File.cfg`:打开File.cfg...
Atom-atom-vim-mode-plus-project-find-from-search.zip,Seamless flow from vmp's search to find-and-replace's project-find未于2017年2月18日从APM注册处发布。,atom是一个用web技术构建的开源文本编辑器。
Sed Basics - Find and Replace Using RegEx 使用`s`命令进行替换。 ```bash sed 's/pattern/replacement/g' filename ``` #### Hack 22: Awk Introduction – Print Examples 使用`awk`打印特定列。 ```bash awk ...
+ s/要替换的内容/替换成的内容/g replace 模式 * 例子:sed -i ‘s/old/new/g’ file awk: * awk 是一种文本处理语言 * 选项: + -F 指明输入时用到的字段分隔符 + -v var=value 自定义变量 * 例子:awk -F: ...
- `vim`:文本编辑器,用于编辑文件。 4. **抽象类与接口的区别** - 继承方式:抽象类使用 `extends` 关键字,接口使用 `implements` 关键字。 - 构造函数:抽象类可以有构造函数,接口则没有。 - 多重继承:一...
- **Search and Replace**: Finding and replacing text within files. - **Advanced Features**: Using features like macros, splits, and plugins. #### Appendix B: Shell Special Characters and Variables ...
- **VIM 和 Emacs:** 主要在 Linux 下使用,适用于高级用户。 #### 七、语法特色 Python 作为一种动态类型语言,具有以下特点: - 动态语言特性:可以在运行时改变对象的属性和方法。 - 缩进方式:使用空格而...
我记得还有个东东叫ViEmu for Word & Outlook,可以在word和outlook中模拟vim,当然可以使用正则式搜索替换了。不过,ViEmu一来也是收费软件(在2008年5月31日之前是79美刀,之后是99美刀),我还没有找到免费版本;...
我记得还有个东东叫ViEmu for Word & Outlook,可以在word和outlook中模拟vim,当然可以使用正则式搜索替换了。不过,ViEmu一来也是收费软件(在2008年5月31日之前是79美刀,之后是99美刀),我还没有找到免费版本;...