Adding tail behaviour where enter adds blank lines to less

I love less, which I can use to follow logs with the +F flag (or the ShiftF hotkey), search forwards and backwards, and generally move freely through the document.

But there is one thing missing in less: usually I am at the end of the file, and I want to see new things happening. In tail -f I would just hit enter several times, and new log lines would just appear clearly separated from old lines.

Is it possible to add this to less? How?


Solution 1:

This isn't really tail's behavior -- it's the terminal. Tail is just sending a stream of output to stdout, which goes to your terminal, and you're inserting blank lines in the middle of that output by hitting enter. Tail itself isn't actually accepting input from you at all. You can type whatever else you want and that will show up too.

less, however, is a console app, and it takes input and processes it. It's managing the console using terminal control sequences, which is how you can scroll around. Letting you insert arbitrary characters into the stream would mess up the display, and anyway less uses your input to control the program.

Unfortunately, it doesn't seem to have a way to mark the current position visually. That's not a terrible feature request, though, and less does still get new features from time to time.

Solution 2:

I found your question while seeking the same answer for myself.

Disappointed by the accepted answer, I came up with a workaround. It's less than ideal, but it allows me to mark my position in the log I'm following with less, which is the whole point.

I created a small executable script (I called it marklog) with the following contents, and put it in my path:

#!/bin/sh
echo >> $1
echo >> $1
echo >> $1
echo `date` ---------------------------------------------------------------------- >> $1
echo >> $1
echo >> $1

Of course, this only works if you have write access to the log file--which could be a deal-breaker in many situations. I've also created this version which I use to write to log files I don't own (but to which I have sudo access):

#!/bin/sh
sudo sh -c "echo >> $1"
sudo sh -c "echo >> $1"
sudo sh -c "echo >> $1"
sudo sh -c "echo `date` ------------------------------------------------------------------------- >> $1"
sudo sh -c "echo >> $1"
sudo sh -c "echo >> $1"

These scripts provide just the sort of visual break I was looking for. There are at least 3 ways you can use them:

  1. At the point where you would normally press enter a few times when using tail -f, instead run marklog from another terminal (providing the path to the log file as an argument).

  2. Use CtrlZ to suspend less so you can run the script in the same terminal window, but when you re-foreground less (using fg, of course), it will no longer be in 'follow' mode, so you'll need to hit ShiftF again...

  3. Lastly--and this might be the most convenient way, because you don't need to type the path to the log file: Run marklog directly from less by typing !marklog %. Less will substitute the current filename for %. However, less won't respond to the ! while it is in 'follow' mode, so you'll have to hit CtrlC first to exit follow mode, run !marklog %, then ShiftF again.

With method 3, you also get the added bonus of of Less's command history: Just hit the ! and then use the up-arrow to select the most recent command (for me, it's always marklog).

Hope this helps someone as much as has already helped me.