1

My scenario is the following: When I ask the psql program for \help, it pipes its output to less. Sometimes, I would like to keep the help contents while writing my next query. I imagine this could happen if I could tell less to write to stdout, like more would do, while in interactive mode.

Restrictions:

I will decide whether or not this should happen AFTER I have seen the contents of \help. So, customizing the pager used by psql is (I think) not an option. This means that my question is not a duplicate of:

https://stackoverflow.com/questions/14474138/customize-pager-in-psql

or:

Making less print to stdout if an entire file can be displayed on one screen

What I am currently using:

\setenv PAGER more OR \setenv PAGER 'less -X'
\help alter user
q (to exit less or more)
-- Now I can write my query while looking at the alter user syntax above

What is lacking in this approach is that I would like to choose whether I want the -X option or not AFTER I have looked at the contents. I can imagine that less can achieve that by writing to both the main and the alternate buffer.

4
  • The manpage indicates that the you can just type in '-X' during interactive mode. Can you try this and see if it works?
    – Haxiel
    Jun 26 at 13:37
  • 1
    Check the | command in the less(1) manpage. For instance, if you type |<Enter>cat<Enter>, you will have the current content left on the screen after you exit or suspend less. Check lesskey(1) if you want to do that with a single key. Jun 26 at 19:13
  • Thanks, @UncleBilly , your comment provides the best solution, because it works without even modifying any settings. If you write it as an answer, I will mark it as accepted. Jun 29 at 10:02
  • Hi, @Haxiel, when I type -X I get: "Cannot change the -X (--no-init) option (press RETURN)" This happens both inside psql and regular bash Jun 29 at 10:04
1

You could provide your own pager script that saves the output as well as giving it to less. Then on exit from less it can choose to show the output again without less, or just exit. For example, assuming the output help is in a pipe and less is not called with arguments:

#!/bin/bash
if tee /tmp/save | less -K
then    cat /tmp/save
fi

The -K makes less quit on control-c and this changes the return code from less to 2 instead of 0. So if you quit less normally the cat will repeat the input, but if you interrupt it, you will get no extra output.

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.