Sublime Forum

Running an OS command within the ST save command?

#1

Hi,

When doing a save of my code within ST I would like to run a small program just before save (as an integral part of save) to generate a build number (as in 1.5.buildnumber) which I then can use in my code as my program’s version number. The major and minor will be manually adjusted based on the changes, but the buildnumber should be automatic. Every time I save the code the build is already triggered in a system I cannot change.

How would I do that? It tried googling on this but it only finds stuff on starting ST from the command line…

0 Likes

#2

Something like this would be possible by creating a plugin that includes an event listener that listens for the event that indicates that a file is (or is about to be) saved. When the event triggers, you can check to see if it’s a file that you care about for your purposes and then take some action.

For example:

import sublime, sublime_plugin

class OnSaveListener(sublime_plugin.EventListener):
    def on_post_save (self, view):
        # Check here to see if view.file_name() is interesting to you based on
        # it's type, location, path, etc.
        print ("saved" + view.file_name ())

        # Execute some external command; you may want to specify "path",
        # "working_dir", etc.
        view.window ().run_command ("exec", {"cmd": "ls"})

This one prints out the name of the file that was just saved and then uses the internal exec command to execute ls for no obvious reason.

Probably you would want to allow for some filtering here; such an event handler will always trigger for every file saved, which would make your external program always trigger. So you may want to do something like examine the file name, it’s location or some project specific setting to see if you actually care about running your external command or not.

on_post_save as it’s name suggests, gets triggered after the file is finished saving. There is also an on_pre_save which triggers before the save happens. This would allow you to e.g. modify the contents of the file being saved before it was written. Depending on what you’re trying to do (and in particular if you really need to run this ‘within the ST save command’) that might be what you want to use instead.

2 Likes

#3

Hi, that looks very promising. But I feel very noob here, I tried a few of the examples when I started looking for plugin development, I can’t get this to work, looks like I am missing something.
General info: https://www.sublimetext.com/docs/3/packages.html
Examples: http://www.sublimetext.com/docs/plugin-examples

So I follow the steps on adding the script to the correct directories, restart ST3 and test the command.
I used the rot13 test example. It says to do this:
“After saving it to a file in Packages/User, you can run it via entering view.runCommand(‘rot13’) in the console (accessible via Ctrl+~).”

When testing it I get this message from the console.

view.runCommand(‘rot13’)
Traceback (most recent call last):
File “”, line 1, in
AttributeError: ‘View’ object has no attribute ‘runCommand’

So I am doing something wrong I guess but don’t have a clue what it is… :frowning:

0 Likes

#4

your second link is very outdated documentation, for ST1 I think…

The API reference shows you need to use view.run_command

1 Like

#5

Thanks, the rot13 stuff is working now, trying on_pre_save now…

Thanks again!

0 Likes

#6

ok, plugin working now, thanks again!

1 Like

#7

Yes, very good, that would be my dev root directory for these type of files (coding lua in Corona).
This should not be run for c#. Although, I might trigger something else for *.cs, an automatic build and test step… :slightly_smiling:

Thanks for pointing in this direction, a whole new world has just opened up!

1 Like

#8

Good to hear!

0 Likes