Home Download Buy Blog Forum Support

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

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

Postby piter011 on Tue Mar 26, 2013 8:04 pm

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?
Last edited by piter011 on Wed Mar 27, 2013 1:04 am, edited 2 times in total.
piter011
 
Posts: 2
Joined: Tue Mar 26, 2013 7:55 pm

Re: Reload file when changed on disk without jumping into tab

Postby adzenith on Tue Mar 26, 2013 10:09 pm

You could write a plugin that calls view.activate() on_post_save... and then reactivates the original view.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: Reload file when changed on disk without jumping into tab

Postby piter011 on Tue Mar 26, 2013 10:51 pm

Thanks for your reply :)
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: Select all
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)

Plugin for saving file and reloading all opened files
Code: Select all
import sublime, sublime_plugin

class SaveAndReloadCommand(sublime_plugin.WindowCommand):
    def run(self):
        self.window.run_command("save")
        self.window.run_command("reload_all_files")

And finally, user key binding:
Code: Select all
{ "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 :)
piter011
 
Posts: 2
Joined: Tue Mar 26, 2013 7:55 pm

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

Postby stinogle on Mon Jun 09, 2014 8:27 pm

Does this plugin work for you in ST3 piter? I can't get it to reload the open files...
stinogle
 
Posts: 2
Joined: Mon Jun 09, 2014 8:25 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 24 guests