Sublime Forum

Run a command when sublime is closed?

#1

I have a command to save all named files (not untitled) but named files.

How can I run this command, when Sublime closes?

No doubt i’ll still get prompted if I have some untitled files, I run my command, then close sublime, and that’s OK. I’ll hit cancel. And deal with it.

But I want to be able to close sublime, and save those named files, without being prompted So I want to run my sublime command(which is written), but I want it to run when sublime is exited.

0 Likes

#2

If you are just trying to save files on exit, have you tried setting “hot_exit” to false?

The description of this setting is

// Exiting the application with hot_exit enabled will cause it to close
// immediately without prompting. Unsaved modifications and open files will
// be preserved and restored when next starting.

So if it’s false, it follows that it would prompt you about unsaved modifications when closing.

0 Likes

#3

There’s no callback when Sublime is closing.

As a workaround, you could bind cmd+q to call your function instead, and then your function could save everything and tell sublime to quit.
Also, if you set a view as scratch, then it won’t ask you to save it when you close it. You could set all your untitled views to scratch mode and they would just be deleted when you close the program.

0 Likes

#4

I would be interested on running the project’s Build command (edit: on exit), but only on a per-project setting. (Some of my text, but not code, projects, have a build command, which basically just commits all changes to the repo. Just so I can go back in time, in case I delete something useful my mistake.) Any thoughts on how I would implement this?

The @adzenith’s method won’t work, because it has to be per-project. (Also, I’ve disabled Ctrl+Q. I use my WM/OS functionality to close sublime.)

Alex

0 Likes

#5

You could just use the same command, and have it check if a project is open before doing anything.
There’s also on_pre_close, which gets fired per file… you could maybe have a counter and if enough files are closing then run your command? You could collect the filenames and run the command on the files on disk, because you aren’t actually able to stop the views from closing.

0 Likes

#6

For what I asked about there’s a plugin: sublime.wbond.net/packages/SublimeOnSaveBuild

0 Likes