惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
GbyAI
GbyAI
aimingoo的专栏
aimingoo的专栏
L
LangChain Blog
I
InfoQ
D
Docker
F
Fortinet All Blogs
Y
Y Combinator Blog
Martin Fowler
Martin Fowler
月光博客
月光博客
B
Blog
Engineering at Meta
Engineering at Meta
T
Tailwind CSS Blog
罗磊的独立博客
博客园_首页
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
D
DataBreaches.Net
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
B
Blog RSS Feed
IT之家
IT之家
V
V2EX

jdhao's digital space

Conversion between base64 and OpenCV or PIL Image 腾讯云对象存储博客图床开启 CDN 加速(不需要购买额外域名) Change Table Column Width in LaTeX Image or Table Side by Side in LaTeX LaTeX 并排显示图像或表格 Firenvim: Neovim inside Your Browser Content inside HTML tags missing in Latest Hugo? Creating Markdown Front Matter with Ultisnips Labelme JSON 标注格式转 voc XML 格式 Nifty Nvim Techniques That Make My Life Easier -- Series 6 macOS 下如何为视频制作字幕 Running Command Asynchronously inside Neovim Resolving Merge Conflict after Git Stash Pop Pylint: command not found? A Hands-on Experience with Neovim's Built-in LSP Support How to Convert PDF to Images with Imagemagick 互联网上常用缩略语集锦 File Backup in Neovim Converting PDF Pages to Images with Poppler Nifty Nvim Techniques That Make My Life Easier -- Series 5 Neovim Configuration for System-wide Use How to sort a list of tuple or list in Python -- lambda or itemgetter? Building A Vim Statusline from Scratch 人类第一颗原子弹爆炸始末 Distributed Training in PyTorch with Horovod Learning Expect Programming Essential Knowledge about SSH Nifty LaTeX Techniques -- Series 1 更改 Adsense 邮寄地址,重新寄送 PIN Mintty Tips and Configurations
Search and Replace in Multiple Files in Vim/Neovim
2020-03-14 · via jdhao's digital space

In Sublime-Text and other editors, we can press Ctrl-Shift-F to invoke the search and replace UI. We can also accomplish this task easily in Vim or Neovim with the built-in features. Here is how.

There are two steps involved in this task. The first step is to find the files containing the search pattern under the current project or directory. The second step is to perform replacement for each file found in 1st step.

Depending on which Vim command to use, there are basically two ways to do this task. For illustrating purposes, we will try to replace all Neovim occurrences in my nvim config repo to Nvim.

Use vimgrep or grep with cfdo#

We can use vimgrep or grep command to populate the Vim quickfix list with files matching our specific pattern. The cfdo command can then be utilized to perform replacement operation for each file in the quickfix list1.

The difference between vimgrep and grep command is that vimgrep is internal and provided by Vim, while grep command uses external programs to search files depending on the platform (see :h grepprg).

vimgrep#

To find all files containing Neovim under current directory using vimgrep, we can use the following the command:

In the above command, Neovim is our search pattern. The meanings of g and j flags are as follows:

  • g: Add all matches in a line to the quickfix list.
  • j: Do not jump the cursor to the location of first pattern match.

The **/* specify the files to search for the pattern. In this case, it means to search recursively under the current directory and for all files types2. You can also search only in certain files types, for example, in Vim script:

:vimgrep /Neovim/gj **/*.vim

After this step, the quickfix will be populated with the matching files, lines and column number (use copen to open the quickfix list):

grep#

If we use grep command, it is often useful to specify the external grep program that we want to use via the grepprg option. For this, I highly recommend ripgrep, which is one of the fastest command line search tools. We need to add the following settings to Neovim or Vim config:

set grepprg=rg\ --vimgrep\ --no-heading\ --smart-case
set grepformat=%f:%l:%c:%m

To search Neovim under current directory using grep, just use grep Neovim.

cfdo#

After the quickfix list is filled with files containing the search pattern, we now use cfdo command to perform replace operation on each file:

:cfdo %s/Neovim/Nvim/ge | update

The cfdo command can be followed by one command or a series of commands (separated by |). In the above example, we first perform substitution, then followed by update command so that the replacement change is saved.

Use args and argdo#

Another way to perform search and replace is to use args and argdo command. The args command is used to build a list of files that we would like to perform an action. argdo command is then used to perform this action on each file in the argument list.

To add files to the argument list, we can use external tools like grep or ripgrep to search files with backtick. For example:

:args `grep Neovim -l -r .`

or

One issue with backtick is that it does not work on Windows. You can only use the above command on Linux or macOS.

After adding all files containing the pattern to argument list, we then use argdo to perform replacement (works similarly to cfdo command):

:argdo %s/Neovim/Nvim/ge | update

References#