Coming from N++, scroll to last line after file updated

I am coming from Notepad++. There is an option that makes the file scroll to the last line after the file changes, is this possible with atom? I tried learning how to write an addon for to do this but couldn’t figure it out.

I assume that you’re only wanting the file to go to the end when the file is changed by some external process and that you don’t want it to scroll to the end when you change the file like when you’re typing?

1 Like

Yep that’s correct. I have a log file open in my second monitor in atom. And as things log it adds to the bottom of the file. I need to watch them. Currently im breaking my fingers trying to scroll haha.

Using Atom as a log viewer isn’t something that Atom is well-designed for. I suspect that you will find that even if you get the “scroll-to-the-end” feature you’re looking for, Atom will fail to work well for you in other ways for this use.

I don’t have an idea off the top of my head for how to implement the feature you’re looking for though I’m quite confident that it could be done.

So, the simplest version of this would just be to move the cursor to the bottom of any non-modified buffer when it is updated. It turns out that onDidChange() fires when a buffer is changed by an external source as well as by Atom.

atom.workspace.observeTextEditors (editor) ->
  editor.onDidChange ->
    if not editor.isModified() then editor.moveToBottom()

It would be pretty easy to go further and limit that to one file extension.

1 Like

Thank you very much sir! I’m very new to n++ and the CoffeScript is confusing me, may you please help me know where to place this?

Drop it into your with the first line unindented. In CoffeeScript, indentation is hugely important, so make sure that any top-level function calls are flush against the left-hand side. If you tell me how your log files look, I can modify it to detect those files.

1 Like

You are too too nice, thank you sir so much! I need it to work with a specific file name “log.txt”. I sincerely appreciate your help! Excuse my delay in reply! :slight_smile:

That’s just a matter of adding another conditional clause.

atom.workspace.observeTextEditors (editor) ->
  editor.onDidChange ->
    if not editor.isModified() and editor.getTitle() is "log.txt" then editor.moveToBottom()
1 Like

Thank you so much testing right now! :slight_smile:

I just tested, its not moving to bottom. I even tested with this:

atom.workspace.observeTextEditors (editor) ->
  editor.onDidChange ->

I recorded screencast to show you please -

Your video is no longer available.

its not moving to bottom.

Is there a blinking cursor in the document? If you click inside the log file, does it start working? moveToBottom() might be dependent on having a cursor, but if this is the case, we can create a cursor.

1 Like

When the file changes my atom doesn’t have focus. But I tried with focus in there it still didn’t work. :frowning:

I really can’t thank you enough for helping me out like this!

Whoops excuse that please. This video should work:

This is weird. It worked in my initial tests, so now I’ve tested it more and it seems to work most of the time, but not always. You can see how it’s supposed to work in this gif when the final line jumps from 39 to 57, but then when I save it again, more log fills up the bottom two lines of the buffer and the cursor remains at 57. I’m not sure why it doesn’t work sometimes.

1 Like

Thank you so much for your image and help there! I’ll keep troubleshooting, I’ll definitely learn something in this. Thanks for exposing me to all this with baby steps!

No problem. If I figure out why it’s providing inconsistent results, I’ll let you know.

1 Like

came here via a web search. Using Atom for logfile monitoring. N++ is for editing. Maybe I got this backwards lol…
Anyway, the snip works.

Not necessarily a good plan, even though I showed someone how to do it a couple of years ago. Log files can get huge, potentially, and Atom has performance issues with huge files. Keep that in mind. :slight_smile:

I get it. Only using Atom at the moment to I can have a monitor dedicated to log file monitoring. N++ is for config file editing.
Maybe a PowerShell tail type command would be better. Dunno. I like Atom. It’s pretty.