8

I often pipe commands to less in order to read through the output (e.g. compiler errors).

MyCommand | less

This is great because it makes trawling through large amounts of output easy, but when I exit less the output is gone. How can I make the output still visible after quitting less?

This question differs from Is there a way to redirect a program's output and still have it go to stdout? because that question relates to output to a text file via tee, which, as far as I know, doesn't provide a facility to split output between less and stdout.

quant
  • 4,161
  • @jasonwryan the question is similar but the accepted answer appears limited to redirecting output to files, at least that's my understanding of the tee command. I am not sure what the rules are regarding this situation but I don't think marking this as a duplicate would benefit the community unless the answer to that question is reviewed. – quant Aug 12 '14 at 23:19
  • @jasonwryan: I agree; they're not the same question. The other one is a shell question; Arman's question is really about less; namely, how to get it to leave the displayed text in the console's scrollable display history buffer. ... P.S. Arman: You might want to [edit] your question and retag is as a /less question. – G-Man Says 'Reinstate Monica' Aug 12 '14 at 23:24
  • @Arman I edited your title & question some more, please feel free to revert my edit if I've misunderstood what you're asking. – derobert Aug 13 '14 at 03:48
  • @derobert that seems fine. – quant Aug 13 '14 at 05:18
  • 3
    They aren't duplicates, they are about totally different things: he wants info about the problematic ncurses behavior of the less command, and this hasn't anything to do with the fd redirections! Shame to you! – peterh Aug 13 '14 at 08:31
  • As a protest against the unfair robo-closing policy here you get an upvote from me. – peterh Aug 13 '14 at 08:32

4 Answers4

14

Using less -X:

Disables sending the termcap initialization and deinitialization strings to the terminal.

That will leave any text on-screen behind before and after paging. So:

command | less -X

will have the effect you want. Note that this output will still be wrong (duplicated lines) if you ever scrolled up - that's unavoidable without without writing to a file.

You can also set the environment variable LESS to a value that contains X to do this by default for every invocation of less.

If you want to write to a file without resorting to tee, you can use the less -o filename or --log-file=filename options.

Michael Homer
  • 76,565
  • 1
    Or if you only realize after starting less that you want to save the output, you can use -o filename interactively (just like most less options). You can't change -X from inside less, though, and it wouldn't be useful because that would leave you in the "alternate" mode of your VT100-style terminal; switching back from that to the normal mode where the scrollback goes is the real reason the screen clears when you exit. – Peter Cordes Jun 09 '17 at 07:29
6

You can use:

 command | tee filename.out | less

and then examine filename.out via an editor if you still need to see the output after running the less command.

Another idea is to change your term setting to use a dumb terminal type. You'd have to experiment with this one.

mdpc
  • 6,834
3

If the issue is just that less is "clearing the screen" when you quit it you can avoid that behavior specifically by telling less not to send the terminal/termcap init/de-init strings when it starts and stops. You do this by using -X. So try command | less -X.

That may have other side-effects if those strings do things that are actually necessary but I don't expect that to be the case for you here.

2

You can also try the command more in place of less. It will still paginate the output and allow you to scroll through it nicely, but in the end your terminal will retain the output as if it were not piped through anything.

Carl
  • 121
  • Since less is more powerful than more, suggesting more is only moderately useful. For instance, with less you can scroll forth and back through the text even it is the output from a pipe - more is not capable of doing that that. – countermode Aug 13 '14 at 14:38
  • I suppose. I suggested it because it fills what the question desired, and no other functionality was mentioned. – Carl Aug 13 '14 at 19:42