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?
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.
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: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):
These scripts provide just the sort of visual break I was looking for. There are at least 3 ways you can use them:
At the point where you would normally press enter a few times when using
tail -f
, instead runmarklog
from another terminal (providing the path to the log file as an argument).Use CtrlZ to suspend
less
so you can run the script in the same terminal window, but when you re-foregroundless
(usingfg
, of course), it will no longer be in 'follow' mode, so you'll need to hit ShiftF again...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 fromless
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 alwaysmarklog
).Hope this helps someone as much as has already helped me.
Press Shift+F at the end of file and less will start to work as
tail -f
.I don't think you can do this. I've used
m
and'
to mark places of interest and then goto them again but this doesn't give you the separation that you ask for. Perhaps something like multitail will do what you want.The answer from Lambart brings me to my favorite solution. Thx to Lambart!
Just interput the follow mode ctrlc and enter
!printf "\n\n---$(date)---\n\n" >> %
. After that you have to press enter one more time and hit shiftf to start again following the file.