106

Relatively often, I find myself wanting to quit less but leave what I was viewing on the screen, to refer back to. Is there any way to do this? Workarounds?

(My current workaround is to quit, then use more. So any workaround that's better than that is welcomed. The ideal would be something I can use once I'm already inside less, perhaps with a shell setting or some scripting.)

My desktop is OSX, but I use RHEL and Ubuntu servers.

  • 2
    Reading a long output (as in git diff or git log) in less -X will take up the scroll buffer in my terminal and evict much of the previous output. I'd love it if less could just exit with the last screenful of output at the time of the exit; i.e. no more than one page of the scroll buffer would be taken after quitting less. Any ideas? – musiphil Jan 09 '14 at 19:57
  • By the way, -R is usually a safer choice than -r. – musiphil Jan 09 '14 at 20:14
  • Thanks. For those wondering: -R is "Like -r, but only ANSI "color" escape sequences are output in "raw" form. Unlike -r, the screen appearance is maintained correctly in most cases." – Steve Bennett Jan 13 '14 at 04:20
  • @musiphil Perhaps -c? – Vanessa Phipps May 01 '14 at 16:46
  • @MatthewPhipps: I guess -c is about how to update the screen when you move around inside less. – musiphil May 01 '14 at 20:48

4 Answers4

102

This is actually a function of the terminal emulator you are using (xterm, gnome-terminal, konsole, screen). An alternate screen, or altscreen, gets launched when programs such as less or vim are invoked. This altscreen has no history buffer and exits immediately when you quit the program, switching back to the original screen which restores the previous window content history and placement.

You can prevent less from launch in an altscreen by passing the argument "-X".

less -X /path/to/some/file

You can also pass "-X" as an environment variable. So if you are using bash, place this in ~/.bashrc:

export LESS="-X"

However, this disbles the termcap (terminal capability) initialization and deinitialization, so other views when you use less may appear off.

Another option would be to use screen and set the option altscreen off in your ~/.screenrc. less will not clear the screen and should preserve color formatting. Presumably tmux will have the same option.

This blog entry describes the problem and offers some different solutions specific to gnome-terminal with varying success.

George M
  • 13,959
  • 2
    Perfect. Is there a downside? – Steve Bennett May 14 '12 at 13:46
  • @SteveBennett Not that I'm aware of. – George M May 14 '12 at 13:47
  • 2
    Wait, I've discovered one already - the output of git log (and probably other coloured commands) is messed up. – Steve Bennett May 14 '12 at 13:49
  • You're right. -X disable the termcap initialization and deinitialization. Hmmm. – George M May 14 '12 at 13:51
  • well, it's still useful - I make a script "les" on my path, which calls less -X $@. And saves a keystroke :) – Steve Bennett May 14 '12 at 13:53
  • @SteveBennett See my edit. Try using "screen" and disabling altscreen. – George M May 14 '12 at 13:58
  • 1
    @SteveBennett That's small enough I'd make it an alias: alias les="/usr/bin/less -X" – bonsaiviking May 14 '12 at 14:33
  • 4
    Hey, I think I just discovered an antidote to the git log issue above: export LESS="-r -X". (No idea what the side-effects of that are...) – Steve Bennett May 14 '12 at 14:43
  • 6
    Actually, git sets the LESS variable to FRSX if it is unset when it runs the pager, so you can just leave LESS unset and less will automatically run with -FRSX. Look for core.pager in git-config(1) for more information. – musiphil Jan 09 '14 at 19:51
  • 1
    This does it for me for now (in cygwin), but my ideal solution would (a) default to not clearing the screen and (b) add to the pager an option to exit removing its output. Obviously (a) is for “Exactly what I wanted to know” and (b) for “_That wasn’t much use_”. – PJTraill Dec 14 '16 at 19:15
  • Awesome!!! My problem was the exact opposite. I run screen and use vim as my editor and it is convenient to sometimes suspend it (CTRL-Z) and see the history, output of compilation and so on. So the solution for me was to add altscreen on in ~/.screenrc. – Alexey Polonsky Jul 18 '17 at 07:01
  • @GeorgeM was there something wrong with my edit of this answer? you make no mention of a problem in your rollback message. – jah Sep 05 '17 at 11:20
  • @jah Your edit did not substantially improve the answer. You added opinion as to what the "best" solution was from a blog post. If you have a solution which you feel is "best", it should be in it's own answer. – George M Sep 05 '17 at 18:49
  • I just sent an email to the creator of less, Mark Nudelman, asking him to weigh in. – Inigo May 04 '21 at 22:58
  • I think we should ask the question to the problem in a slightly different way.

    How to make less re-print the last screen after the -X deinitialization strings to the terminal before it exit ?

    So that we don't have to copy is ahead or to use two terminal

    – Dennis C Dec 02 '21 at 03:17
  • @SteveBennett a downside is that this disables mouse scrolling. You can often re-enable it with less -X --mouse – Jwosty Sep 30 '23 at 05:35
23

The way I remember this is less -SEX for when I need to dump output to the screen but don't want lines to wrap. For example docker ps | less -SEX What that does is this:

  • -S
    • Scroll instead of wrap
    • If you drop the -E, you can use your arrow keys to scroll
  • -E
    • Exit when you reach the EOF
  • -X
    • Prevent term swapping/blanking
    • The "memory" part is that I know what S and E does, so this must be the other part. (And our reason for committing this command to memory is that we want to dump (not enter an interactive session) unwrapped output.

If you can't remember less -SEX, there's not a lot of hope for you. Just re-google every time I guess.

Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232
7

George's solution didn't work for me, but this solution did (from the blog entry linked in his answer).

  1. $ infocmp -I xterm > ~/xterm-noclear.src

  2. Edit ~/xterm-noclear.src

    • change the name on the second line from 'xterm' to 'xterm-noclear', or whatever suits you (also change 'xterm-debian' if it's present)

    • remove the instructions 'smcup' and 'rmcup' (e.g. "smcup=\E[?1049h, " and "rmcup=\E[?1049l, ")

  3. $ mkdir ~/.terminfo

  4. $ tic ~/xterm-noclear.src (x/xterm-noclear should appear in your ~/.terminfo directory)

  5. $ export TERM=xterm-noclear (now check the behaviour of less and, if satisfied, add the export directive line to your ~/.profile)

(I copied these instructions directly from @jah's rejected edit of George's answer.)

prl
  • 168
4

There is an easier way. Just pipe the command to "cat" command

less blah.txt|cat 

and it works not only for "less" but also other similar command which create a "new" terminal to print the output (git log for example)

  • Not quite the full solution. It doesn't work if you're piping into less. For example cat some_long_text_file.txt | less | cat the cat chases its tail. – lacostenycoder Feb 05 '21 at 11:08