Sublime Forum

Auto reload and go to last line

#1

Hello,

I like the feature auto reloading of a changed file in ST. My case, I am observing log files. I would like ST to go to last line after auto reloading.

I couldn’t find any parameters. So I want to ask in here to be sure.

Is there a parameter to make ST to go to last line after auto reload?

Thanks & Regards,
Ertan

0 Likes

#2

You can make use of the on_reload or on_reload_async API’s provided by ST in a small plugin. You can save this plugin in a python (.py) file (The name of the file is upto you) in the User directory. Since this is a ViewEventListener, you don’t need to do anything extra.

import sublime
import sublime_plugin

class GotoLastLineReloadListener(sublime_plugin.ViewEventListener):

    @classmethod
    def is_applicable(cls, settings):
        syntax = settings.get("syntax")
        return syntax == "Packages/Text/Plain text.tmLanguage"

    def on_reload_async(self):
        self.view.run_command("goto_line", { "line": -1 })

This assumes of course that those log files of yours are simple plain text files. When the log files reload, this plugin will ensure any applicable files will show the cursor on the last line of the file.

2 Likes

#3

I appreciate the help.
Log files are indeed plain text files. I just have log highlighting turned on them.
I tried your code and so far could not make it work.
I am not sure that I could install it correctly though. Used, Tools->Developer->New plugin and paste and save the file.

0 Likes

#4

If you’re using syntax highlighting then you’re not using the plain text syntax, so the plugin is not triggering.

While you have one of your log files opened and syntax highlighted, open the console and do view.settings().get("syntax") and modify the code in is_applicable() to compare against that syntax instead of the one that’s above.

1 Like

#5

Thank you.
That did make it work.

0 Likes

#6

Hi! Nice it’s works!, but…how can activate it and disable it from Command Palette? Thanks!

0 Likes

#7

To do that you would need to adjust the plugin; Currently the ViewEventListener marks itself as applicable for the current file only based on syntax. It would need to also check for some setting that you make up yourself (e.g. go_to_eol_on_reload) and see that it’s set to True. Then you’d need a command to turn that flag on or off in the current file, and add that command to the command palette.

In your use case, would this be always enabled and then you turn it off, or always off and then you turn it on when you care?

0 Likes