Find And Replace plugin for neovim
![image](https://private-user-images.githubusercontent.com/95201/329157403-770900e2-36c6-488c-9117-5fcb514454cb.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI0NzUzNjgsIm5iZiI6MTcyMjQ3NTA2OCwicGF0aCI6Ii85NTIwMS8zMjkxNTc0MDMtNzcwOTAwZTItMzZjNi00ODhjLTkxMTctNWZjYjUxNDQ1NGNiLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAxVDAxMTc0OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTEwMzQwMTIxNzA2M2UxZGMyZWE3MTMxOTliMTM1NWYwNmVjZDM0OWQ3MzMwODU0NzYyYzZmOGVjMjhjNDVmNmMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.vn-FbaCeMOBnD1rFq5LeNNIAuI4qhkUHk2LA8NrCCaI)
Grug find! Grug replace! Grug happy!
- Search using the full power of
rg
- Replace using almost the full power of
rg
. Some flags such as--binary
and--json
, etc. are blacklisted in order to prevent unexpected output. The UI will warn you and prevent replace when using such flags. - Open search results in quickfix list
- Goto file/line/column of match when pressing
<Enter>
in normal mode on lines in the results output (keybind configurable). - Inline edit result lines and sync them back to their originating file locations using a configurable keybinding.
- Manual/auto-save search history and reload
![image](https://private-user-images.githubusercontent.com/95201/333847675-66afe574-f9a7-4aa8-8fbd-2ad763b4b323.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI0NzUzNjgsIm5iZiI6MTcyMjQ3NTA2OCwicGF0aCI6Ii85NTIwMS8zMzM4NDc2NzUtNjZhZmU1NzQtZjlhNy00YWE4LThmYmQtMmFkNzYzYjRiMzIzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAxVDAxMTc0OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTk5NmM3ZGE3Y2I1OGViYmUwYmIyYTFlNDg1MjgxOWRlNTgwMGU1YWYyMTRmZjY0YzVjZTZjNGUxY2IzOWEzNzgmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.pRmxg-bx9zjyRJxeS7LMtoO91OK2r6H_mCeZRNwxg-8)
![image](https://private-user-images.githubusercontent.com/95201/335840749-a768c853-c5d7-497c-8138-08285a5c5714.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI0NzUzNjgsIm5iZiI6MTcyMjQ3NTA2OCwicGF0aCI6Ii85NTIwMS8zMzU4NDA3NDktYTc2OGM4NTMtYzVkNy00OTdjLTgxMzgtMDgyODVhNWM1NzE0LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAxVDAxMTc0OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTYyNjYxMjRkMTBiNDg1ZDQxZjg5NTIyMGNmZGI2MTk1ZTA4NmFlODI1MmUzN2Y2MmRmZDYxMGUxMTU5ZjVkYTUmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.dAFaK_RwnUh9xIPADSOQl_180gLN5elSIRBTBuytLW0)
![image](https://private-user-images.githubusercontent.com/95201/333847729-97651c2c-ea18-4b2d-b735-b52996903803.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI0NzUzNjgsIm5iZiI6MTcyMjQ3NTA2OCwicGF0aCI6Ii85NTIwMS8zMzM4NDc3MjktOTc2NTFjMmMtZWExOC00YjJkLWI3MzUtYjUyOTk2OTAzODAzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAxVDAxMTc0OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPTAyYzFmNGVjMTM0MWYwOWIzMTAyMDBmN2NiZGJmNGNmM2I1MWUzOTYzNzY3YTlmZDFiZDk4OWM2ZDliZjFiNTMmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.W26Tba5c5atJmoLBGCH5MLvz7Yf4i87a6EHSfzDmZUg)
![image](https://private-user-images.githubusercontent.com/95201/333847752-f1f25cd0-bbc6-414c-b0e4-a2be4a1b10e3.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI0NzUzNjgsIm5iZiI6MTcyMjQ3NTA2OCwicGF0aCI6Ii85NTIwMS8zMzM4NDc3NTItZjFmMjVjZDAtYmJjNi00MTRjLWIwZTQtYTJiZTRhMWIxMGUzLnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAxVDAxMTc0OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPThkN2IwMmY2MzA2N2ExNzliMmQ1NmRmYjA0NDNhZWU4YzQyOTYyM2NmNTExYmM1MjBjZmEwNzIyMjM5NzYxODAmWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.VevYG0dpKIvUuUjC_7SuSZpN5H3p8gQLq33qqPRohOA)
![image](https://private-user-images.githubusercontent.com/95201/335840800-c24f0678-640e-4dae-bc00-002fca1c4607.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MjI0NzUzNjgsIm5iZiI6MTcyMjQ3NTA2OCwicGF0aCI6Ii85NTIwMS8zMzU4NDA4MDAtYzI0ZjA2NzgtNjQwZS00ZGFlLWJjMDAtMDAyZmNhMWM0NjA3LnBuZz9YLUFtei1BbGdvcml0aG09QVdTNC1ITUFDLVNIQTI1NiZYLUFtei1DcmVkZW50aWFsPUFLSUFWQ09EWUxTQTUzUFFLNFpBJTJGMjAyNDA4MDElMkZ1cy1lYXN0LTElMkZzMyUyRmF3czRfcmVxdWVzdCZYLUFtei1EYXRlPTIwMjQwODAxVDAxMTc0OFomWC1BbXotRXhwaXJlcz0zMDAmWC1BbXotU2lnbmF0dXJlPWY1NjQzMGM5ZTg2NGEzMTg0MTU3MDk4N2FiNzZkNWFhMzA3ZGE0NzBmNGMwN2MwOWVlZWNiNDBhZGVlZDBhYzImWC1BbXotU2lnbmVkSGVhZGVycz1ob3N0JmFjdG9yX2lkPTAma2V5X2lkPTAmcmVwb19pZD0wIn0.g6eVn1FWdCmSLa0b6Hby2PhyXVLk2hn-AXPE8S7hJIo)
- strives for reduced mental overhead. All actions you can take are in your face. As much help as possible is in your face (some configurable). Grug often forget how to do capture groups or which flag does what.
- transparency. Does not try to hide away
rg
and shows error messages from it which are actually quite friendly when you mess up your regex. You can gradually learnrg
flags or use existing knowledge from running it in the CLI. You can even input the--help
flag to see the fullrg
help. Grug like! - reuse muscle memory. Does not try to block any type of buffer edits, such as deleting lines, etc. It's very easy to get such things wrong and when you do, Grug becomes unable to modify text in the middle of writing a large regex. Grug mad!! Only ensures graceful recovery in order to preserve basic UI integrity (possible due to the magic of extmarks). Recovery should be simple undo away.
- uniformity. only uses one tool,
rg
, and does not combine with other tools likesed
. One should not have to worry about compatibility differences when writing regexes. Additionally it opens the door to use to many fancyrg
flags such as different regex engine that would not be possible in a mixed environment. Replacement is achieved by runningrg --replace=... --passthrough
on each file with configurable number of parallel workers.
- Neovim >= 0.9.5 (might work with lower versions)
- BurntSushi/ripgrep
- a Nerd Font (optional)
Run :checkhealth grug-far
if you see unexpected issues.
Using lazy.nvim:
{
'MagicDuck/grug-far.nvim',
config = function()
require('grug-far').setup({
... options, see Configuration section below ...
... there are no required options atm...
});
end
},
grug-far.nvim comes with the following:
Note on the key mappings: By default, grug-far, will use <localleader>
for it's keymaps as that is the vim
recommended way for plugins. See https://learnvimscriptthehardway.stevelosh.com/chapters/11.html#local-leader
So to use that, make sure you have <localleader>
configured. For example, to use ,
as the local leader:
vim.g.maplocalleader = ','
You can open a new grug-far.nvim vertical split buffer with the :GrugFar
command. But possibly
best to map a keybind to it for easy triggering.
Since it's just a buffer, you can edit in it as you see fit. The UI will try to guide
you along and recover gracefully if you do things like ggVGd
(delete all lines).
Ultimately it leaves the power in your hands, and in any case recovery is just a few u
taps away.
You can create multiple such buffers with potentially different searches, which will reflect in each buffer's title (configurable). The buffers should be visible in the buffers list if you need to toggle to them.
Search and replace is accomplished by simply typing text on appropriately marked lines. Search will happen in a debounced manner as you type. In the options, you can also specify a minimum number of characters that one has to enter before search is triggered. You can also specify a files filter to narrow down your search and more ripgrep flags to refine it further. Error messages from ripgrep when entering invalid flags and so on are displayed to guide you along.
Note: When replacing matches with the empty string, you will be prompted to confirm, as the change is not
visible in the results area due to UI considering it just a search. If you
would like to see the actual replacement in the results area, add --replace=
to the flags.
It is possible to sync the text of the lines in the results area back to their originating files.
This operation is either done on the current cursor line (Sync Line
), or on all lines (Sync All
).
A sync will happen only if a line has changed in some way compared to the source file, so if there's either a replacement taking place or you have manually edited it.
Deleting result lines will cause them to be excluded from being synced by Sync All
action.
This can be a nice way to refine a replacement in some situations if you want to exclude a particular file
or some particular matches.
If you don't edit the results list, Sync All
and Replace
have equivalent outcomes, except for one case.
When you do multi-line replace with --multiline
and --multiline-dot-all
flags, sync won't work so you
have to use replace. Essentially the difference it that Replace
runs rg --replace=... --passthrough
on
each file and does not depend at all on what's in the results area. Sync All
does a line by line
sync based on what's in the results area.
Note: changing the <line-number>:<column>:
prefix of result lines will disable sync for that line
Note: sync is disabled when doing multiline replacement (--multiline
flag)
Note: if you would like sync to work when doing a replacement with empty string, please add --replace=
to the flags.
Result lines can be opened in the quickfix list. Deleting result lines will cause them not to be included.
Note: changing the <line-number>:<column>:
prefix of result lines will remove lines from consideration
Note: quickfix list is disabled when doing multiline replacement (--multiline
flag)
grug-far can keep track of your search history. This is done either by manually adding a history entry with
History Add
action or automatically on certain successful actions like Replace
and Sync All
.
When you would like to pick one of your history entries to reuse, you can use the History Open
action to
open the search history as a buffer. From there you can either pick an entry that will be auto-filled in.
Note that you can edit the history buffer and save just like any other buffer if you need to do some cleanup. The format of a history entry is:
<optional comment, ex: My special search>
Search: <text>
Replace: <text>
Files Filter: <text>
Flags: <text>
History entries are separated by one or more empty lines.
Note: grug-far will ignore lines that do not start with the prefixes above
If you inadvertently launched a wrong search/sync/replace, you can abort early using the Abort
action.
When you are done, it is recommended to close the buffer with the configured keybinding
(see Configuration section above) or just :bd
in order to save on resources as some search results
can be quite beefy in size. The advantage of using the Close
action as opposed to just :bd
is that it
will ask you to confirm if there is a replace/sync in progress, as those would be aborted.
Note that grug-far.nvim buffers will have filetype=grug-far
and history buffers will have filetype=grug-far-history
if you need filter/exclude them in any situations.
For more control, you can programmatically open a grug-far buffer like so:
require('grug-far').grug_far(opts)
or if you would like to pre-fill current visual selection as the search text:
(note, this will also set --fixed-strings
flag as selection can contain special characters)
require('grug-far').with_visual_selection(opts)
where opts
will be merged with and override the global plugin options configured at setup time.
See here for all the available options
For more details on the API, see docs
:lua require('grug-far').grug_far({ prefills = { search = vim.fn.expand("<cword>") } })
:lua require('grug-far').grug_far({ prefills = { flags = vim.fn.expand("%") } })
:<C-u>lua require('grug-far').with_visual_selection({ prefills = { flags = vim.fn.expand("%") } })
:lua require('grug-far').toggle_instance({ instanceName="far", staticTitle="Find and Replace" })
vim.api.nvim_create_autocmd('FileType', {
group = augroup('test'),
pattern = { 'grug-far' },
callback = function()
vim.keymap.set('n', '<localleader>w', function()
local state = unpack(require('grug-far').toggle_flags({ '--fixed-strings' }))
vim.notify('grug-far: toggled --fixed-strings ' .. (state and 'ON' or 'OFF'))
end, { buffer = true })
end,
})
- nvim-spectre: the OG find and replace in a buffer plugin, great inspiration!
- telescope.nvim: lifted
rg
healthcheck from there :P - lazy.nvim: used their beautiful
README.md
as a template - plugin-template.nvim: super handy template, this plugin is based on it!