Sublime Forum

"Save all" improvement

#1

Context:

  1. Multiple files are opened
  2. Some files are already available on the disk drive but were changed by lets say a find/replace in all opened files.
  3. Some files are just temporary files created with “File>New File” but never saved.

User action:
The user clicks “File>Save all”

Background:
Temporary files cannot be saved automatically since path is unknown.

Issue:
ST3 starts to save each opened file and stops at first temporary file, prompting the user to save the temporary file.
If the user cancels (most of the time I don’t want to save my temporary files - they are just temporary), the whole loop stops and remaining files are not saved.

Improvement:
First save files with known paths and after that save temporary files.

7 Likes

#2

I see no reactions to my post, but there are some readers.
Is the issue description readable and clear?
Can somebody reproduce the issue?

0 Likes

#3

yus, I can reproduce on Windows

0 Likes

#4

You can add this code in your key bindings.

{ "keys": ["ctrl+alt+s"], "command": "save_all" }

and you are good to go.

0 Likes

#5

Thanks, but this does not solve the issue. It just adds a keyboard shortcut to the menu command but the behavior is the same: Not possible to solve until temporary files are manually saved.

0 Likes

#6

A potential solution to your problem might be a plugin similar to the following. This implements a new command save_all_existing that works like save_all does, but only triggers the save for files that already have filenames, skipping over the files that don’t have names (since you mentioned you often don’t want to save them).

The EventListener will rewrite all save_all commands to the new one, in which case the current menu item and any key binds you might have will just transparently use the new command.

If you like, you can drop that part of the plugin and keep just the command, and then create a key binding, menu entry and/or command palette entry that runs the new command so that you can have it both ways.

import sublime
import sublime_plugin


class SaveAllExistingCommand(sublime_plugin.WindowCommand):
    def run(self):
        for view in self.window.views():
            if view.file_name() is not None and view.is_dirty():
                view.run_command("save")


class SaveAllListener(sublime_plugin.EventListener):
    def on_window_command(self, window, cmd, args):
        if cmd == "save_all":
            return ("save_all_existing", args)
1 Like

Save All except new files?
Is there an API event when a file gets deleted from the filesystem but is still open in a buffer?
#7

Thanks for the code!
I wanted to test it and had to read about how to work with plugins.
I pasted the code into a new plugin an saved it in C:\Users\Administrator\AppData\Roaming\Sublime Text 3\Packages\User\save_all\save_all.py

After that, the save_all command still behaves as usual. Do I have to do something else?

0 Likes

#8

Sublime only automatically loads plugin files at the top level of a package. If you move it up one directory level (so it’s directly in User instead of User\save_all), that should be all you need to do.

0 Likes

#9

Great, it works now. Thank you.

I have modified your plugin, so that the temp files are saved after all other files are saved (see below).
This way, I can stil have the option of saving temp files if I want or I can simply cancel.
Furthermore if would be nice to have a “cancel all” button in the pop up window.
Finally, I think this feature should be available in Sublime Text in the core. That would be cool.

import sublime
import sublime_plugin

class SaveAllExistingCommand(sublime_plugin.WindowCommand):
def run(self):
for view in self.window.views():
if view.file_name() is not None and view.is_dirty():
view.run_command(“save”)
for view in self.window.views():
view.run_command(“save”)

class SaveAllListener(sublime_plugin.EventListener):
def on_window_command(self, window, cmd, args):
if cmd == “save_all”:
return (“save_all_existing”, args)

0 Likes

#10

For those on macOS, the path for saving new plug-ins is ~/Library/Application Support/Sublime Text 3/Packages/User.

0 Likes