Sublime Forum

[SOLVED] Reload file when changed on disk without tab focus

#1

Hello,
Is there any way to force Sublime Text to automatic file reload when it is changed on disk? By “automatic” I mean that I don’t have to navigate to the tab with opened file. So I have 2 tabs opened and in one of them I have for example SASS file and in second CSS file generated from the SASS one. When I press CTRL+S and save changes in SASS file, CSS file is recompiled (by external app) and I want to see the changes immediately in second tab without jumping into it.

Does anybody know how to achieve it?

0 Likes

#2

You could write a plugin that calls view.activate() on_post_save… and then reactivates the original view.

0 Likes

#3

Thanks for your reply :smile:
Could you give some more details how to do it?
I’m not Python developer but I assume that it should be fairly easy to add that kind of feature to ST2 and it’s very important to me, because I’m preparing SASS video course and it would be great if I could show everything on-the-fly in ST.

EDIT:
Success! You gave me a good hint and then I started to dig in Sublime Text API and “How to write a plugin” tutorials. I made two plugins and everything works as expected, but it’s some kind of a workaround. Anyway, thanks for help! Code that I wrote is below. Maybe someone will use it:

Plugin for reloading all opened files (all tabs)

[code]import sublime, sublime_plugin

class ReloadAllFilesCommand(sublime_plugin.WindowCommand):
def run(self):
# Run command after 1sec (i.e. wait for SASS to compile CSS)
sublime.set_timeout(self.focus_all_views, 1000);

def focus_all_views(self):
    window = self.window
    # Remember current view
    current_view = window.active_view()
    # Save group number
    gr_number = window.num_groups()

    # Loop through all groups
    for i in range(0, gr_number):
        # Focus group
        window.focus_group(i)
        # Save views in that group
        views_in_i = window.views_in_group(i)
        # Focus all views here
        for inner_view in views_in_i:
           window.focus_view(inner_view)

    # Back to current view
    window.focus_view(current_view)

[/code]
Plugin for saving file and reloading all opened files

[code]import sublime, sublime_plugin

class SaveAndReloadCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.run_command(“save”)
self.window.run_command(“reload_all_files”)[/code]
And finally, user key binding:

{ "keys": "ctrl+s"], "command": "save_and_reload" }

Maybe it’s not the best possible solution, but like I said - I’m not Python programmer and it’s my first ST2 plugin :smile:

0 Likes

#4

Does this plugin work for you in ST3 piter? I can’t get it to reload the open files…

0 Likes