In Sublime Text 2, I want to be able to save all open/loaded files that have names.
I like how Sublime can have files with filenames, and have files that were never saved, and can be closed and it remembers about the untitled files and reloads them without me having to save them.
But when a file has a filename and has some changes in the buffer not yet saved, sublime shows it as such, with the filename and circle, I close sublime, and reopen it, I sublime has remebered it as it was and so the changes are still not saved to the file. That's great.. But.. I'd like a command to save all, but not the untitled ones.
There is a save all option in the menu, but it pops up a dialog box asking regarding saving of untitled files.
What API functions would be involved to write a command that leaves the untitled ones as is, and saves the ones with filenames? (and is there any example code I can run that uses those API functions?)
AFAIK, an opened file is represented by one or more views. So try to get all views and save those with file names. I wrote a simple example. Hope it can help you.
By the way, you can check all API's via the following link.
Sublime Text 2 API Reference
import sublime, sublime_plugin
class SaveAllExistingFilesCommand(sublime_plugin.ApplicationCommand):
def run(self):
for w in sublime.windows():
self._save_files_in_window(w)
def _save_files_in_window(self, w):
for v in w.views():
self._save_exiting_file_in_view(v)
def _save_exiting_file_in_view(self, v):
if v.file_name():
if v.is_dirty():
v.run_command("save")
lhuang's answer is fantastic, and does exactly what I think you want it to do. Make sure you save the plugin as Packages/User/save_all_existing_files.py
in order for it to work properly. You can reach the Packages
directory via the Preferences -> Browse Packages...
menu item. I do have a few additions to make your life a little easier, though - a menu item and a key combination.
You generally shouldn't edit anything in the Packages/Default
directory, as all the files can be overridden/expanded upon, but in this case I recommend it for aesthetics sake. Open Packages/Default/Main.sublime-menu
and add the following line right after line 128, which should be the save_all
menu item:
{ "command": "save_all_existing_files", "caption": "Save All Named Files", "mnemonic": "F" },
This will add a "Save All Named Files" option to the File
menu. Next, go to Preferences -> Key Bindings - User
and add the following line:
{ "keys": ["ctrl+alt+shift+s"], "command": "save_all_existing_files" },
If this file doesn't have any other contents, surround the above line with square brackets [ ]
and remove the trailing comma, otherwise Sublime will complain at you.
Once the keymap file is saved, you can trigger the command by hitting CtrlAltShiftS. Obviously, you can change the keys if you don't like them. If you pick a combination that gives weird behavior, check out the FindKeyConflicts
plugin by @skuroda - it's invaluable for troubleshooting your setup, and especially when developing your own packages.
Good luck!
Just add the next line to the Preferences > Key Bindings - User
{ "keys": ["super+shift+s"], "command": "save_all" }
super
is a Command key in OS X. Use ctrl
on Windows.