ripgrep integration in nvim
As usual using your plugin manager, e.g. lazy.nvim
local P = {
'doums/rg.nvim',
cmd = { 'Rg', 'Rgf', 'Rgp', 'Rgfp' },
}
return P
require('rg').setup({
-- Optional function to be used to format the items in the
-- quickfix window (:h 'quickfixtextfunc')
qf_format = nil,
-- Glob list of excluded files and directories when the special
-- `e` flag is set (it uses the `--glob !*` flag of rg)
excluded = {
'.idea',
'node_modules',
'.git',
'target',
'package-lock.json',
'Cargo.lock',
},
})
Each of the following commands spawn rg
with the supplied arguments
and then populates the quickfix list with the match(es)
NOTE Do not quote the PATTERN argument as when using rg
in a
terminal. It is automatically quoted by the plugin as expected.
⚠ Positions of command arguments are strict
Make a rg search with defaults, in the current directory
:Rg a pattern
Make a rg search with defaults, in the provided path
:Rgp a pattern /a/path
Make a rg search with flag(s), in the current directory
Available flags:
I
→--no-ignore
H
→--hidden
h
→--hidden
and glob list of excluded files and directoriesS
→--smart-case
s
→--case-sensitive
i
→--ignore-case
e
→ glob list of excluded files and directories
:Rgf HIs a pattern
Make a rg search with flag(s), in the provided path
Same flags as Rgf
:Rgfp HIs a pattern /a/path
The plugin module exposes the following methods. Each of them
spawn rg
with the supplied arguments and then populates the
quickfix list with the match(es)
rg(pattern
: string, flags
: listOf[IHSsiE], path
: string)
Example
require('rg').rg('a pattern', { 'H', 'I' }, '/a/path')
rgui(path
: string?)
Uses vim.ui.*
interface to query the flag(s) and the pattern. It
can take an optional path as argument.
Useful when combined with a file explorer plugin to search under a specific path.
Example with nvim-tree
In the files'tree window press <C-f>
to search in the
directory/file under the cursor
-- nvim-tree config
local function on_attach(bufnr)
-- ...
vim.keymap.set('n', '<C-f>', function()
local node = api.tree.get_node_under_cursor()
require('rg').rgui(node.absolute_path)
end, opts(''))
end
Mozilla Public License 2.0