105

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.

5
  • 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 9, 2014 at 19:57
  • By the way, -R is usually a safer choice than -r.
    – musiphil
    Jan 9, 2014 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." Jan 13, 2014 at 4:20
  • @musiphil Perhaps -c? May 1, 2014 at 16:46
  • @MatthewPhipps: I guess -c is about how to update the screen when you move around inside less.
    – musiphil
    May 1, 2014 at 20:48

4 Answers 4

101

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.

16
  • 2
    Perfect. Is there a downside? May 14, 2012 at 13:46
  • 2
    Wait, I've discovered one already - the output of git log (and probably other coloured commands) is messed up. May 14, 2012 at 13:49
  • 1
    @SteveBennett That's small enough I'd make it an alias: alias les="/usr/bin/less -X" May 14, 2012 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...) May 14, 2012 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 9, 2014 at 19:51
21

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.

2
  • 13
    The guy has a problem and you offer him less sex?
    – yossico
    Jul 23, 2019 at 12:39
  • export LESS="-SEX"
    – scharfmn
    Nov 9, 2021 at 18:45
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.)

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)

1
  • 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. Feb 5, 2021 at 11:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .