How to edit multiple files through "Find results" in Sublime Text
Asked Answered
T

2

16

When I search multiple files via Command + Shift + F, the result is returned as something like a text file. This text file is editable, but changes made don't affect the original files.

Is it possible to do such that changes in "Find Results" propagates to the original source file?

Tawny answered 7/11, 2013 at 20:10 Comment(3)
As far as I know this is not possible - I have tried to find anything with similar functionality.Redemptioner
@MikkoOhtamaa Now it is. See my answer.Nainsook
replace in files with regex and capture groups can be useful (you can even make a search project with specific selected or ignored files or folders)Levy
N
20

The Find Results Apply Changes plugin was created to do just that.

You can install it through Package Control's "Install Package" option.

Before using this plugin, make sure that you have UTF-8 encoding enabled using the menu:

File > Save with Encoding > UTF-8

Once installed, you can apply any change you made to a "Find Results" buffer back to the files:

  • Search for "foo" in a folder (Sublime's default shortcut is CTRL+SHIFT+S)
  • This will open a "Find Results" buffer listing all the files with "foo" in it.
  • Change the instances of "foo" for "bar" or something else...
  • Go to the menu:

    Find > Find Results - Apply Changes
    
  • This will write all the changes made back to the files and save the modified files automatically.

By default, using menus is the only way to make it work. However, it is quite tiresome and doesn't save as much time as it does with a keyboard shortcut. You can set your chosen keyboard shortcut by adding a new line in

Preferences > Key Bindings - User

by adding:

{ "keys": ["ctrl+r"], "command": "find_results_apply_changes" },

Don't use CTRL+S as it will overwrite saving file shortcut.

Warning!: According to the author of the plugin:

Uses regions to allow you do multiline changes, but when inserting new newlines, will corrupt files if you commit more than once, this because the new newlines will shift the line numbers. Will also 'corrupt' files if you add/remove newlines in other instances of the modified files. eg in another tab. To prevent corruption this packages will alert you and prevent most of these.

(This is a modified version of the description from the Find Results Apply Changes Github page.)

Nainsook answered 7/5, 2014 at 14:24 Comment(3)
Thanks, but this killed double-click nav from the find results for me.Berryberryhill
A good plugin ruined by author's biased view. See their WONTFIX section for details. Practically unusable on Windows (or any system where line ending matter and are not same as unix line endings).Reger
Not on ST3? This makes me very 😢Vesicant
N
2

The shortest workaround I can think of would be to open the target file from search results by simply double clicking the path and then jumping to the according line using Ctrl+G on Windows or +G on Mac OS.

That's the way I do it and must say it is only a matter of seconds, even without the plugin.

Newfoundland answered 8/11, 2013 at 9:50 Comment(2)
Yes, but imagine 'search and replace' option with search results file or something like this when you need to change something in 100 files.Tawny
If you want to change the same string in those 100 files, you can use the Replace in files instead of Find in files option. (Command+Shift+F in the bottom right corner).Newfoundland

© 2022 - 2024 — McMap. All rights reserved.