1

I'd like to pipe a process to less, and then issue a command to the scroll the end of the current piped text. That is, shift+G blocks if the command hasn't yet closed its stdout, so for a long-running command you can't see the end until it finishes.

As an example:

i=0; while true; i=$((i+1)); do echo $i; sleep 0.001; done | less

If you hit shift+G once less starts paging, it doesn't scroll down and just blocks indefinitely.

Note that I'm not worried about the buffering - the output is frequent enough that the buffer doesn't cause much delay. Rather, I'd like less not to hang when scrolling to the end of the current buffer (note that it hangs before updating the display, so I do not even see the end of the buffer).

  • Would piping it to tail and use [Shift][Up][Down] be a good workaround? – Fabby Nov 14 '19 at 22:33
2

The way I deal with this is to write the output to a file and then page the file. Using your example, this would be

i=0; while true; i=$((i+1)); do echo $i; sleep 0.001; done >/tmp/somefile
less /tmp/somefile

The disadvantage is that depending on the process generating output, /tmp/somefile can grow arbitrarily large.

The reason for writing/reading with a file rather than through a pipe is pragmatic. The less utility has a different response depending on whether it is reading from a pipe or a file. When reading from a file it "knows" the end of the file and can seek immediately to it. If the file later grows, another seek-to-end (G) will redetermine the end point and reseek to that new point. On the other hand, seeking to end of a pipe will block until the pipe is closed. (This blocking effect applies regardless of whether the pipe is buffered or unbuffered.) Once less has blocked it will only respond to Ctrl C, which also hits the process generating the output to be viewed. Unfortunately this is usually counterproductive.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.