2

I usually do not want to reset the terminal when I exit less (that is, I want the screen to continue displaying what I was looking at). Usually, when I'm on a new box and haven't configured it yet, I invoke less, quit, see the screen reset, curse mildly, and rerun with less -X ... On very rare occasions, I remember that the screen is going to reset as soon as I quit, in which case the sequence of events is slightly modified and the mild cursing occurs before I quit, but the overall effect is about the same.

Is there any way to modify a currently running less process so that it will not reset the terminal upon exit?

1
  • short - no. long - see my answer. Commented Oct 25, 2018 at 23:20

2 Answers 2

3

Some terminals (such as xterm) have a menu setting which enable/disables switching to/from alternate screen. Most do not...

By the way (same issue of some/most), if a terminal implements the original xterm 47 code alternate-screen switching, it is possible to switch back into the alternate screen without clearing it. I've noticed some which set TERM=xterm which do not implement this (only implementing the later 1049 code) - ymmv

You can see how the 47 code is used in a normal terminal description by looking at the smcup and rmcup capabilities from

infocmp xterm-old 

which shows these items (among others):

rmcup=\E[2J\E[?47l\E8, smcup=\E7\E[?47h, 

The \E[2J in rmcup clears the (alternate) screen before switching back to the normal screen. The \E7 and \E8 save/restore the cursor position (for the normal screen). If you really wanted to, you could replace those capabilities with whatever xterm-compatible terminal description, omitting the clear-screen part, allowing you to do

tput smcup tput rmcup 

from the command-line and see what was on the alternate screen. It wouldn't be nice (since your command-line would overwrite parts of it), but it might help.

It's not needed in xterm, since xterm has a menu option for switching back/forth. But you might find that approach useful.

Further reading:

3

use | . cat.

Stepwise: press |, then at the |mark: prompt press ., then at the ! prompt enter cat, then at the |done (press RETURN) prompt press Enter again; when you quit less, the current page we be left on the screen.

If you want the output to be processed as it was by less (with backspace+underscore transformed to underline, non-printable characters shown as <XX>, etc), use less -FX instead of cat.

from the less(1) manpage:

 | <m> shell-command <m> represents any mark letter. Pipes a section of the input file to the given shell command. The section of the file to be piped is between the first line on the current screen and the position marked by the letter. <m> may also be ^ or $ to indi- cate beginning or end of file respectively. If <m> is . or new- line, the current screen is piped. 

Afterwards, if you want to simply zap the rmcup/smcup alternate screen switching without having to reconfigure again and again every xterm clone, you can use this command:

infocmp | sed 's/[sr]mcup=[^=]*,//g' | tic - 

This will write inside ~/.terminfo a terminal description for your current terminal with the codes for alternate screen switching omitted. It should be used in preference to that from /usr/share/terminfo by most programs.

5
  • That prints the file which less is rendering, but it's not the same, e.g., if it contains non-printing characters (and does not affect the reset). Commented Oct 25, 2018 at 23:24
  • @ThomasDickey I hope I addressed your objections in the last revision. Commented Oct 25, 2018 at 23:44
  • That's better (writing an approximation to the normal screen), though as noted, you can't modify the reset from within less :-) Commented Oct 25, 2018 at 23:47
  • This is ingenious. To make sure I understand what's happening, less writes to the alternate screen and the shell out to cat writes to the normal screen? Commented Oct 25, 2018 at 23:59
  • @WilliamPursell exactly. Commented Oct 26, 2018 at 0:01

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.