Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 860
Adam Pietrasiak Avatar asked Nov 07 '13 20:11

Adam Pietrasiak


People also ask

How do you edit multiple lines in Sublime Text?

To select multiple regions using the keyboard, select a block of text, then press Ctrl+Shift+L to split it into one selection per line. When you're done with using multiple selections, just press Ctrl+K to trim all but the first.

How do I use global search in Sublime Text?

Use the Search all shortcut: Ctrl + Shift + F , then select the folder in the "Where:" box below. (And for Mac, it's ⌘ + Shift + F ). If the root directory for the project is proj, with subdirectories src and aux and you want to search in all subfolders, use the proj folder.

How do I change all occurrences in Sublime Text?

In Sublime Text, you can edit all occurrences of a word by highlighting the words with alt+F3 , and then typing whatever you want to change the word to.

How do I open multiple files in Sublime Text?

To do so, you need to press Ctrl+K, then Ctrl+Shift+Up. You can repeat as many times as you like. Press Ctrl+K, then Ctrl+Shift+Up to open a new sub-window.


1 Answers

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.)

like image 106
Rafal Avatar answered Sep 21 '22 20:09

Rafal