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