psql: recall previous command?

Started by Neil Conwayalmost 21 years ago11 messages
#1Neil Conway
neilc@samurai.com

Is there a way to recall the previous command in psql? Obviously, "up
arrow" or Ctrl-P using readline and the default readline bindings is
close, but it recalls the previous _line_ of input. That is not at all
the same thing in the case of a multiple line command, for example.

If there is no way at present, I think there should be. Using "up arrow"
is quite annoying when dealing with multiple line SQL statements.

Two issues:

- how to handle slash commands? Slash commands and SQL statements can be
intermixed:

neilc=# select 1 \timing
Timing is off.
neilc-# ;
?column?
----------
1
(1 row)

So I'm not quite sure what the right behavior here is. We could always
just ignore slash commands (the command would "recall the previous SQL
statement") -- since few slash commands are multi-line, I don't think
that would be too bad.

- when a multiple-line command is recalled, how should it be presented
in the psql input buffer? Perhaps we could strip newlines from recalled
command text, so that the recalled command would fit on a single line.
That would mean the recalled command would look somewhat different than
how the user typed it, although of course the semantics of the query
should be the same. Any better ideas?

-Neil

#2Alvaro Herrera
alvherre@dcc.uchile.cl
In reply to: Neil Conway (#1)
Re: psql: recall previous command?

On Tue, Feb 22, 2005 at 03:26:52PM +1100, Neil Conway wrote:

Is there a way to recall the previous command in psql? Obviously, "up
arrow" or Ctrl-P using readline and the default readline bindings is
close, but it recalls the previous _line_ of input. That is not at all
the same thing in the case of a multiple line command, for example.

Something else to be considered about readline behavior: prepending a #
on M-# is useless. -- in that context would be much better, or /* if in
a multiline command.

(Just something I have had in my to-do list for a while ...)

--
Alvaro Herrera (<alvherre[@]dcc.uchile.cl>)
"La felicidad no es ma�ana. La felicidad es ahora"

#3Ian Barwick
barwick@gmail.com
In reply to: Neil Conway (#1)
Re: psql: recall previous command?

On Tue, 22 Feb 2005 15:26:52 +1100, Neil Conway <neilc@samurai.com> wrote:

Is there a way to recall the previous command in psql? Obviously, "up
arrow" or Ctrl-P using readline and the default readline bindings is
close, but it recalls the previous _line_ of input. That is not at all
the same thing in the case of a multiple line command, for example.

Just checking: do you mean something like the \e and \p commands?

test=> SELECT \timing
Timing is off.
test-> 'Hello World';
?column?
-------------
Hello World
(1 row)

test=> \p
SELECT
'Hello World';
test=>

Ian Barwick

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Neil Conway (#1)
Re: psql: recall previous command?

Neil Conway <neilc@samurai.com> writes:

- when a multiple-line command is recalled, how should it be presented
in the psql input buffer? Perhaps we could strip newlines from recalled
command text, so that the recalled command would fit on a single line.

That fails on at least three cases: -- comments, newlines separating
parts of quoted literals (cf bizarre SQL spec requirement that adjacent
literals are merged only if separated by a newline), and newlines
embedded in quoted literals. You could possibly work around the first
two cases by doing nontrivial editing of the query string, but I see no
way around the last one.

regards, tom lane

#5Darcy Buskermolen
darcy@wavefire.com
In reply to: Neil Conway (#1)
Re: psql: recall previous command?

On February 21, 2005 08:26 pm, Neil Conway wrote:

Is there a way to recall the previous command in psql? Obviously, "up
arrow" or Ctrl-P using readline and the default readline bindings is
close, but it recalls the previous _line_ of input. That is not at all
the same thing in the case of a multiple line command, for example.

Currently I use \e to bring up the last command (multiline) in the editor,
from this point I can just save it to execute it.

If there is no way at present, I think there should be. Using "up arrow"
is quite annoying when dealing with multiple line SQL statements.

Two issues:

- how to handle slash commands? Slash commands and SQL statements can be
intermixed:

neilc=# select 1 \timing
Timing is off.
neilc-# ;
?column?
----------
1
(1 row)

So I'm not quite sure what the right behavior here is. We could always
just ignore slash commands (the command would "recall the previous SQL
statement") -- since few slash commands are multi-line, I don't think
that would be too bad.

- when a multiple-line command is recalled, how should it be presented
in the psql input buffer? Perhaps we could strip newlines from recalled
command text, so that the recalled command would fit on a single line.
That would mean the recalled command would look somewhat different than
how the user typed it, although of course the semantics of the query
should be the same. Any better ideas?

-Neil

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

--
Darcy Buskermolen
Wavefire Technologies Corp.
ph: 250.717.0200
fx: 250.763.1759
http://www.wavefire.com

#6Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Neil Conway (#1)
Re: psql: recall previous command?

Is there a TODO here?

---------------------------------------------------------------------------

Neil Conway wrote:

Is there a way to recall the previous command in psql? Obviously, "up
arrow" or Ctrl-P using readline and the default readline bindings is
close, but it recalls the previous _line_ of input. That is not at all
the same thing in the case of a multiple line command, for example.

If there is no way at present, I think there should be. Using "up arrow"
is quite annoying when dealing with multiple line SQL statements.

Two issues:

- how to handle slash commands? Slash commands and SQL statements can be
intermixed:

neilc=# select 1 \timing
Timing is off.
neilc-# ;
?column?
----------
1
(1 row)

So I'm not quite sure what the right behavior here is. We could always
just ignore slash commands (the command would "recall the previous SQL
statement") -- since few slash commands are multi-line, I don't think
that would be too bad.

- when a multiple-line command is recalled, how should it be presented
in the psql input buffer? Perhaps we could strip newlines from recalled
command text, so that the recalled command would fit on a single line.
That would mean the recalled command would look somewhat different than
how the user typed it, although of course the semantics of the query
should be the same. Any better ideas?

-Neil

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#7Thomas F.O'Connell
tfo@sitening.com
In reply to: Neil Conway (#1)
Re: psql: recall previous command?

One interesting artifact of using \e to edit a multi-line command is
that the same command is then treated as a single-line command in
subsequent up-arrow or Ctrl-P attempts.

I use this frequently to achieve a similar effect to what you're after.
The one downside is that if you leave the psql session, it's returned
to multi-line format in the psql history.

-tfo

--
Thomas F. O'Connell
Co-Founder, Information Architect
Sitening, LLC
http://www.sitening.com/
110 30th Avenue North, Suite 6
Nashville, TN 37203-6320
615-260-0005d

On Feb 21, 2005, at 10:26 PM, Neil Conway wrote:

Show quoted text

Is there a way to recall the previous command in psql? Obviously, "up
arrow" or Ctrl-P using readline and the default readline bindings is
close, but it recalls the previous _line_ of input. That is not at all
the same thing in the case of a multiple line command, for example.

If there is no way at present, I think there should be. Using "up
arrow"
is quite annoying when dealing with multiple line SQL statements.

Two issues:

- how to handle slash commands? Slash commands and SQL statements can
be intermixed:

neilc=# select 1 \timing
Timing is off.
neilc-# ;
?column?
----------
1
(1 row)

So I'm not quite sure what the right behavior here is. We could always
just ignore slash commands (the command would "recall the previous SQL
statement") -- since few slash commands are multi-line, I don't think
that would be too bad.

- when a multiple-line command is recalled, how should it be presented
in the psql input buffer? Perhaps we could strip newlines from
recalled command text, so that the recalled command would fit on a
single line. That would mean the recalled command would look somewhat
different than how the user typed it, although of course the semantics
of the query should be the same. Any better ideas?

-Neil

---------------------------(end of
broadcast)---------------------------
TIP 8: explain analyze is your friend

#8Neil Conway
neilc@samurai.com
In reply to: Bruce Momjian (#6)
Re: psql: recall previous command?

Bruce Momjian wrote:

Is there a TODO here?

Probably -- I think there is definitely room for improving psql's
handling of multi-line queries. However, \e works well enough for me,
and I don't think I'll get around to looking at this for 8.1. So feel
free to add a TODO item.

-Neil

#9Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Neil Conway (#8)
Re: psql: recall previous command?

Neil Conway wrote:

Bruce Momjian wrote:

Is there a TODO here?

Probably -- I think there is definitely room for improving psql's
handling of multi-line queries. However, \e works well enough for me,
and I don't think I'll get around to looking at this for 8.1. So feel
free to add a TODO item.

And what would the TODO item be? "Improve psql's handling of multi-line
queries" is too vague.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
#10Neil Conway
neilc@samurai.com
In reply to: Bruce Momjian (#9)
Re: psql: recall previous command?

Bruce Momjian wrote:

And what would the TODO item be? "Improve psql's handling of multi-line
queries" is too vague.

If you can include a link to the archives or the text of the relevant
mails, it seems fine to me. I'm not sure specifically _how_ we want to
improve the handling of multi-line queries, and it seems silly to make
that decision now just for the sake of a specific TODO item description.

-Neil

#11Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Neil Conway (#10)
Re: psql: recall previous command?

Neil Conway wrote:

Bruce Momjian wrote:

And what would the TODO item be? "Improve psql's handling of multi-line
queries" is too vague.

If you can include a link to the archives or the text of the relevant
mails, it seems fine to me. I'm not sure specifically _how_ we want to
improve the handling of multi-line queries, and it seems silly to make
that decision now just for the sake of a specific TODO item description.

OK, I will just add the wording.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073