On 19th of November 2019, Thomas Munro committed patch:
Allow invisible PROMPT2 in psql. Keep track of the visible width of PROMPT1, and provide %w as a way for PROMPT2 to generate the same number of spaces. Author: Thomas Munro, with ideas from others Discussion: https://postgr.es/m/CA%2BhUKG%2BzGd7RigjWbxwhzGW59gUpf76ydQECeGdEdodH6nd__A%40mail.gmail.com
So, I guess you know that psql has prompting variables:
- PROMPT1 – used when psql is waiting for you to type in command
- PROMPT2 – used when you entered already part of the command, but then you pressed enter without ; at the end, so psql expects you to continue with the query
- PROMPT3 – used when you're in process of COPY FROM STDIN
For example, with default psql:
depesz=# SELECT 1,
depesz-# 'a',
depesz-# 'b';
?COLUMN? | ?COLUMN? | ?COLUMN?
----------+----------+----------
1 | a | b
(1 ROW)
Please note that prompts in lines 2 and 3 are different from line 1 (depesz= vs. depesz–).
This difference can be a bit too minimal to notice properly.
You can always, for example:
depesz=# \SET PROMPT2 '>> '
depesz=# SELECT 1,
>> 'a',
>> 'b';
?COLUMN? | ?COLUMN? | ?COLUMN?
----------+----------+----------
1 | a | b
(1 ROW)
But the width of prompt in subsequent lines is different making reading of the query harder.
Now, with this new change, we can:
depesz=# \SET PROMPT2 '%w'
depesz=# SELECT 1,
'a',
'b';
?COLUMN? | ?COLUMN? | ?COLUMN?
----------+----------+----------
1 | a | b
(1 ROW)
Which is great because not only are subsequent query lines easier to read, the whole thing is now copy-paste-able without any edition.
Great stuff, thanks to all involved.
Thanks for this cool article.
Can you show the values of PROMPT1, PROMPT2 & PROMPT3 used, please?
Do you need to provide a “static” number of space to get the `PROMPT3` configuration OK?
@Thomas B:
My PROMPT1/2/3 in these examples are basic, just like in “psql -X”. So you can start psql, and then do \set – and you’ll see them all.
Not sure what you mean by PROIMPT3 configuration OK. I generally never modify PROMPT3, i just keep it as default ‘>> ‘
this should incorperate the current length of the prompt2, since i use %R quite a lot, which tells me if there are for example open parenticies.
this is my prompt setup:
🐘 15:53:47 » @:5432/ # select (
🐘 1 ( » true, false )
🐘 2 – » ;
and the existing 8 chars on PROMPT2 should be incorporated to get the offset for %w
the commentsection butchers my prompt 😀
if you want to get the full picture, here is my prompt config: https://gist.github.com/verfriemelt-dot-org/2e0136d62cbfeb7ce67f14b0731512b0