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

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?

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

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.

Related

How to make a buffer have a read-only in Sublime Text 2

This is not about those files that have their read-only flag set at the OS level, but about every file that users don't intend to modify.
I want Sublime Text to ignore any changes and prevent saving anything to such files. One example for this scenario is when the user is reading source code that shouldn't be altered in anyway.
"Just be really careful, and don't press any buttons" is undoubtedly a good advice, but if I were to "accidentally" delete that octothorpe in front of a comment, or add new lines to a file that is sensitive to such things (some config files in Linux) and then accidently hit save....
I found "toggle-readonly" at GitHub, but it is actually toggling the file permissions ("Read Only", "Write"), which is not quite what I wanted.
Yes, this is possible, but you'll have to write a plugin (which actually isn't that hard, especially if you know Python). The API call is view.set_read_only(flag) in the sublime module, where Flag is a boolean. Here's a quick example which checks if a newly-opened file has a certain suffix, and if so sets it to read-only.
import sublime
import sublime_plugin
class MakeViewReadOnlyCommand(sublime_plugin.TextCommand):
def run(self, edit):
if self.view.file_name().endswith(".cfg"):
self.view.set_read_only(True)
class ConfigFileListener(sublime_plugin.EventListener):
def on_load(self, view):
view.run_command("make_view_read_only")
Open a new file with Python syntax, copy the code into it, alter it as needed, then save it in your Packages/User directory as make_view_read_only.py. Restart Sublime to load it, and you should be all set. To test if a certain view is read-only, open the console and enter
view.is_read_only()
The plugin "Toggle the View Read-Only" will do it. It basically does what MattDMo said: when you set the view as read-only, the file can still be changed by another program (or another user), and Sublime Text will pick up those changes. It also has the context menu item you asked for. I like the "Readonly" indicator in status bar.
I didn't test it on Sublime Text 2, but in Sublime Text 3 it works great, and it claims to work on Sublime Text 2 as well.

How to negate 'remove all folders' mistake in Sublime Text project

In Sublime Text (2 & 3) I find I accidentally remove all folders from the project when I don't want to (this option is poorly placed in the menu, with no obvious undo or warning and is arguably similar to a 'clear' button on a form).
I often have many folders open in a project each one a leaf in the folder tree structure, which is my workflow, so naturally this is a nasty break in my work if it's accidentally triggered!
I would like to know if I can either disable this option or undo it if I accidentally trigger it?
Aside from a backup, a version control system, or a versioning feature on your file system, there is unfortunately no way of undoing the "Remove all Folders from Project" command, because as soon as the command is fired the folders are removed from the .sublime-project file, and the file is saved. However, there is a way to disable the command. The methods vary between Sublime Text 2 and 3, so I'll go over 2 first.
In Sublime Text 2, click on Preferences -> Browse Packages... to open the Packages folder, whose location varies by operating system. Go into the Default folder, and open Main.sublime-menu in Sublime (it's a JSON file). Search for "close_folder_list" and find the line that looks like this (it's line 737 in version 2.0.2):
{ "command": "close_folder_list", "caption": "Remove all Folders from Project", "mnemonic": "m" },
Now, you can either simply delete the entire line, or comment it out by putting // as the first characters on the line. Save the file, then click on the Project menu to see that the option is gone.
If you're using Sublime Text 3, you'll need a workaround to access the Packages/Default folder and its contents, as in this version most of the packages that you would normally have seen in the Packages directory in ST2 are zipped into .sublime-package files and stored elsewhere. However, there's a plugin for that! Make sure you have Package Control installed, then open the Command Palette, type pci to bring up Package Control: Install Package, and search for PackageResourceViewer. Install it, open the Command Palette again, type prv, and select PackageResourceViewer: Edit Package Resource. Scroll down to Default, click on it or hit Enter, then scroll down to Main.sublime-menu and select it to open it for editing. You can now follow the instructions above to find the line containing "close_folder_list" (it should be line 795) and either delete it or comment it out.
If you'd like to keep the menu item, but move it to a different spot, you can do that as well. For example, if you'd like it at the very bottom of the menu, separated by a divider, delete the original line, put the cursor below the "refresh_folder_list" line, and paste in the following:
{ "caption": "-" },
{ "command": "close_folder_list", "caption": "Remove all Folders from Project", "mnemonic": "m" },
so it looks like this:

Sublime text Autosave only for specific files

I don't want to hit cmd+s all day when I code a web UI using Sublime Text 2.
SCENARIO:
Sublime Text with LiveStyle plugin
Google Chrome with Live Style addon
WHAT I DON'T WANT:
Hitting cmd+s every time I edit an html/js/css file
Applying a global autosave solution like "save_on_focus_lost": true to all files of a project (e.g. python or php files shouldn't be autosaved)
WHAT I WANT:
Autosave and automatically reload only certain files of my project in Chrome (thanks to LiveStyle)
QUESTION:
How to selective apply an autosave setting (or plugin) like "save_on_focus_lost": true only to certain files within a project?
Any alternative solution is welcome
Don't know anything about LiveStyle, so I'll speak to the autosaving specifically. If it's all files of a particular type, you can create a syntax specific setting. If it isn't you can write a plugin that ties into the on_load or on_activated event. When this event is triggered, you can apply a setting to the view. Assuming you have the view, you can do something like view.settings().set("save_on_focus_lost", True) in the plugin.

Sublime text 2 - Zip open files with full path name and dir structure

Is there a plugin or a macro, that creates a tar ball or a zip of all opened files / tabs in Sublime Text 2.
If not, how to make a plugin that does that?
I was checking the API of Sublime Text 2, only to know that there is no method that gets the full path of all open files.
However, I see that we have an option when we right click in any file - "Copy file path".
Which method it calls? Can I replicate the method to all tabs switching one by one? If yes, then how to cycle through open tabs one by one using the API?
You can use window#views to get all the views (as a list). Then iterate on each view and use view#file_name to get the files. As for creating zips, take a look at the zipfile library.
For your particular case, I would create a window command. You can then bind that to a key combination or add it as a command palette entry. Finally, to set the visible view, you can use window#focus_view. You may also need window#focus_group if you have multiple groups.

How to edit preference file for Notepad++?

My open source project uses spaces, not tabs, in its code.
A contributor to the project has his own website which uses tabs, not spaces.
He uses Notepad++ in Windows to edit files for both projects, and is frustrated when he forgets to manually set the Preferences correctly and thus inserts tabs into my project's files.
Is there a way to edit this Preference via a script? That way he could click one icon to start Notepad++ in mode 1 (running a batch script to modify the Preferences file and then start the program) or click another to start in mode 2.
Or if there's any other way to make him not have to remember this chore, that would work too. Maybe having a Preferences setting that depends on what folder the edited file is inside of?
Any help would be appreciated. Thanks!
The notepad++ setting for replacing tabs with spaces is stored in following xml file
%USERPROFILE%\Application Data\Notepad++\config.xml
The following is the line that needs to be changed
<GUIConfig name="TabSetting" size="4" replaceBySpace="yes" />
Now I don't know how we can edit a file by batch script in windows, but if it is not possible, then he can keep two config files config-tabs.xml and config-spaces.xml. And in the script you can copy appropriate file depending on what he needs to work on.