nvim-lua / wishlist

A public catalogue of Lua plugins Neovim users would like to see exist
MIT License
235 stars 0 forks source link

Search and replace across multiply files #18

Closed Shatur closed 3 years ago

Shatur commented 3 years ago

What? VSCode have a very convinient way to replace string across multiply files: image In this menu, you can filter found by a specific file type, delete unsuitable entries and replace. And all this in real time with a preview of the result.

Why? In Neovim, you can use Telescope / FZF / grep + Quickfix window + Cfilter. But this approach have some limitations:

  1. You cannot replace strings while preserving case. (e.g. replace FOO with BAR and foo with bar)
  2. There is no convinient way to remove a specific file from the search (which, for example, in the middle), but to apply all other changes.
  3. There is no live preview: image
  4. There is no search history in Telescope / FZF (but will be added in Telescope with this PR).

So, it would be awesome to a more convinient solution.

Potential existing implementations: Unfortunately, I do not know of any similar plugin, neither in VimScript nor in Lua.

RRethy commented 3 years ago

This can be done with the quickfix list, :grep, and the standard plugin cfilter (bundled with Neovim).

Shatur commented 3 years ago

This can be done with the quickfix list, :grep, and the standard plugin cfilter (bundled with Neovim).

Of course, that's exactly what I meant (just forgot to mention cfilter). But this method is not very convenient. In VSCode I can do the same with live preview and much faster.

ThePrimeagen commented 3 years ago

So there are plenty of options here to do this.

  1. You need a set of items that are the things you wish to replace. vimgrep, FzF, or Telescope can all do this.
  2. You pipe them to a quickfix list. For telescope its C-q, I think its A-tab in FzF (i don't use), and vimgrep goes directly to QF list
  3. Use cdo. cdo can even do confirm.

Example) Here is the things to do. I have Telescope hooked up and I search for PerformanceHand. Selection_121

Results Are presented Selection_122

Press C-q to pipe to QF list Selection_123

CDO Selection_124

Accepting Each Change (drop the c to not do that) Selection_125

Confused about QuickFix Lists and You wish to see a shameless Self Promo?

Here is a video on QF lists.

More Shameless Than Telescopic Johnson

Shatur commented 3 years ago

You need a set of items that are the things you wish to replace. vimgrep, FzF, or Telescope can all do this. You pipe them to a quickfix list. For telescope its C-q, I think its A-tab in FzF (i don't use), and vimgrep goes directly to QF list Use cdo. cdo can even do confirm.

This is the way I mentioned in the issue message. At the moment, I use exactly the same approach to replace things ​​in files in Neovim. I also want to mention the very handy quickfix-reflector.vim plugin that allows me to edit things inside Quickfix and write to files on save.

But such approach have some limitations:

  1. You cannot replace strings while preserving case. (e.g. replace FOO with BAR and foo with bar)
  2. There is no convinient way to remove a specific file from the search (which, for example, in the middle), but to apply all other changes.
  3. There is no live preview: image
  4. There is no search history in Telescope / FZF (but will be added in Telescope with this PR).

Perhaps I should have mentioned this right away. Updated the first post.

So, of course you can do anything in Vim without plugins, but sometimes they make life easier. Therefore, I proposed the idea of ​​a plugin.

RRethy commented 3 years ago
  1. https://github.com/tpope/vim-abolish
  2. There are multiple options, you can use cfilter or just exclude that file as an option to the :grep command
  3. fzf and telescope no?
  4. <c-j>/<c-k> in fzf?
Shatur commented 3 years ago
  1. How can I use it with cfdo?
  2. You are right, but for CFilter I need to copy filename and add exact match (not very convinient). Also yes, I can exclude it from grep, but not from FZF / Telescope.
  3. No, FZF / Telescope will show found occurences, I'm talking about replace preview.
  4. Thanks, didn't know about it.
RRethy commented 3 years ago
  1. You can do a :cfdo %S/original/replacement/g.
Shatur commented 3 years ago

You can do a :cfdo %S/original/replacement/g.

Didn't know it works this way. Thank you! Preserving case was most important to me. Perhaps I will just add custom keybindings for CFilter and will continue to replace strings in this way.

RRethy commented 3 years ago

https://github.com/windwp/nvim-spectre

This just got posted to r/neovim and might be closer to vscode experience.

Shatur commented 3 years ago

https://github.com/windwp/nvim-spectre

Looks interesting, will try it out, thanks!

dudicoco commented 11 months ago

Is there a way to use the search term input from telescope's live_grep within the quickfix list? I would like to set up a keymap which will open cdo with the search term within a search replace statement:

cdo s/<live_grep_input>//gc