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


#1

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 atom.io to do this but couldn’t figure it out.


#2

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?


#3

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.


#4

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.


#5

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.


#6

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?


#7

Drop it into your init.coffee 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.


#8

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:


#9

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()

#10

Thank you so much testing right now! :slight_smile:


#11

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

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

I recorded screencast to show you please -


#12

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.


#13

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:


#14

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.


#15

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!


#16

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