Sublime Forum

Auto-scroll to bottom (tail log files)

#1

I’m often using Sublime Text to view log files. Sometimes these logs files are still being written into by some apps.

I’d like to make Sublime Text to always scroll to the bottom of the growing file, aka tail the log file.

Is there some way how to achieve this? Maybe some package?

Thanks

0 Likes

#2

I’m not sure if there’s a package for it or not currently, nor a setting (at least nothing that is coming to mind).

In theory, a plugin could implement an on_load and on_reload event listener that checks to see if the file just loaded (or reloaded) is a *.log file, and if so execute the command that jumps to the bottom of the file.

Depending on what you’re trying to do that may or may not be desirable without some additional controls though; if you were trying to look back through the log you would constantly get janked back to the bottom, for example.

0 Likes

#3

@OdatNurd Thanks for your reply. In some text fields I’ve seen behavior to keep scrolling to the bottom only if you are currently scrolled to the bottom (the cursor is on the last line, usually the last empty line). That makes it easy to interrupt the scrolling if needed - just click somewhere where it’s not the last line.

I’m not sure if I will be able to program my own plugin, but if nothing like that really exists, it sounds like a good first project.

0 Likes

#4

I don’t have any free time at the moment (ramping up to a big deployment at work so time is at a premium), but if nobody else jumps in, this seems like an interesting thing to play with creating in my next stream, which will be Friday.

The jump to the bottom portion is pretty easy (in that relative way); finessing the cursor position location after a reload may be more fraught though…

0 Likes

#5

@OdatNurd I had no idea there was a YouTube channel that dedicated to Sublime Text. I’m a big fan of the editor and I also watch YouTube a lot. I’m going to checkout your Twitch streams as well. I’m not a Python programmer, but it does sound interesting. Thank you.

1 Like

#6

We worked on this in live stream yesterday evening as well as this evening, and came up with the plugin in the gist linked below.

This currently requires Sublime Text 4 and to be in your User package OR in a package which has a .python-version file that selects that package to run in the 3.8 plugin host.

It detects when a file is a log file based on the extension, and enables itself in that file (though there is a command you can use to disable it on a file by file basis).

When active, if a reload happens and there is a single, non empty selection within the last line of the file, the view will jump to the bottom of the new content after the reload.

Note that you should not hand edit a log file while using this if you can help it; that will change where the last line in the file is such that the next reload won’t be able to detect it.

The plugin could in theory handle the on_modified event to re-save where the last line is, though that would have to be debounced which could also leave things in an inconsistent state in some cases.

All else being equal, if you’re examining a live log file, you probably don’t want to edit it directly anyway.

0 Likes

#7

This looks really great. Thank you for all your effort and time to implement this.

I’m sorry to bother you with an amateurish question, but could you please elaborate a bit more about how to set it up?

You said that this needs to be in the User package. I do have the latest stable Sublime Text 4. I downloaded the raw file and saved it as log_file_tail.py into $Env:USERPROFILE\AppData\Roaming\Sublime Text\Packages\User. But I am not sure it’s what you really meant. I don’t see the command appear in the command pallette even when opening a *.log file.

If you can point me to some resources to learn more about the setup, I’d be grateful (is there maybe an interesting video on one of your channels?). So far, I only have experience with the Package Control commands, and haven’t dealt with custom py scripts.

0 Likes

#8

I watched your stream and I think I get it now. You weren’t talking about commands in the command pallette, but in the console instead.

Anyway, thanks again.

0 Likes

#9

It’s not mentioned explicitly, but the command is just a regular command.

So, you can create a key binding on it or add it to the command palette, or both if you’d like (you can also add it to the menu).

The command name that you see me manually triggering in the console is the one you’d bind a key to, for example.

If you’d like, I can provide some details on adding it to the menu; for adding to the command palette:

0 Likes