66

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.

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

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.

  • 2
    Perfect. Is there a downside? – Steve Bennett May 14 '12 at 13:46
  • 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
  • 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
  • 3
    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
  • 5
    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 '14 at 19:51
4

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

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.

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.