This is an automated archive made by the Lemmit Bot.
The original was posted on /r/neovim by /u/lukas-reineke on 2024-04-10 05:08:06.
There was a discussion about search and replace in Neovim yesterday, and some people seemed to not know about :help grep
. I’ll use that as an excuse to write about it.
First of all, :grep
will search for a regex pattern in the files specified, and populate the quickfix list with the result. From there, you can do a lot of things, one of which is search and replace. The quickfix list is extremely powerful. To get familiar with it, I recommend reading :help quickfix.txt
.
Now, grep
is not very fast. We can do a lot better today by using ripgrep. And it is super easy to use it from within Neovim by changing :help 'grepprg'
and :help 'grepformat'
.
vim.opt.grepprg = "rg --vimgrep"
vim.opt.grepformat = "%f:%l:%c:%m"
With this in your init.lua
, :grep
will use ripgrep
instead. ripgrep
works slightly different from normal grep
in that it does not take a second file parameter, but instead just searches in the current directory. So to use it, all you have to do is run :grep foo
.
Some other tips.
- To get rid of the annoying message output, run it with
:help :silent
,:sil grep foo
ripgrep
has a lot of useful options. I personally use--hidden
to include hidden files, and--smart-case
, which works the same as:help 'smartcase'