Subject: Re: psql command line editing

From: Ron Johnson <[email-masked]>
Date: Wed, 12 Feb 2025 13:55:04 -0500

On Wed, Feb 12, 2025 at 1:50 PM Rich Shepard <[email-masked]>
wrote:

> My web searches suggest that using the psql command line I'm limited to
> moving the cursor one character at a time. Is there a way to use a small
> editor, e.g., joe on linux, to move by words or to the begining and end of
> the line?

The PGDG prebuilt packages are built with libreadline.  Thus, if that's how
you installed psql, Home, End, Ctrl-Left, Ctrl-Right and Ctrl-w should work.

-- 
Death to <Redacted>, and butter sauce.
Don't boil me, I'm still alive.
<Redacted> lobster!

---

From: Tom Lane <[email-masked]>
Date: Wed, 12 Feb 2025 14:29:15 -0500

> you installed psql, Home, End, Ctrl-Left, Ctrl-Right and Ctrl-w should work.

On my machine, Ctrl-Left/Right don't seem to do anything, but
the usual Emacs combinations Ctrl-A, Ctrl-E, Escape-B, Escape-F
and so on work in psql.  I don't believe I did anything special
to configure that.  Check your local readline documentation.

As Adrian notes, there's also \e, which is good for entering
or editing multi-line queries.

			regards, tom lane

---

From: Rich Shepard <[email-masked]>
Date: Wed, 12 Feb 2025 11:44:29 -0800 (PST)

On Wed, 12 Feb 2025, Tom Lane wrote:

> On my machine, Ctrl-Left/Right don't seem to do anything, but the usual
> Emacs combinations Ctrl-A, Ctrl-E, Escape-B, Escape-F and so on work in
> psql. I don't believe I did anything special to configure that. Check your
> local readline documentation.

Tom,

I run psql from a urxvt virtual console, not emacs (which is where I write
scripts.)

> or editing multi-line queries.

Often when updating table rows I'll modify one string and the row ID so I'll
learn to use \e to move around the string.

Many thanks,

Rich

---

From: Adrian Klaver <[email-masked]>
Date: Wed, 12 Feb 2025 11:50:49 -0800

> I run psql from a urxvt virtual console, not emacs (which is where I write

The point Tom was making is that psql uses readline for it's editing 
positioning and that for him Emacs movement commands work in psql.

-- 
Adrian Klaver
[email-masked]

---

From: Rich Shepard <[email-masked]>
Date: Wed, 12 Feb 2025 11:54:47 -0800 (PST)

> positioning and that for him Emacs movement commands work in psql.

Adrian,

Thanks for clarifying; the emacs commands work for me, too, in psql.

Regards,

Rich

---

From: Ron Johnson <[email-masked]>
Date: Wed, 12 Feb 2025 15:03:53 -0500

> or editing multi-line queries.

There's got to be something in your shell config which says "use emacs
keys" instead of "use vi keys".

---

From: Adrian Klaver <[email-masked]>
Date: Wed, 12 Feb 2025 12:29:35 -0800

On 2/12/25 12:03, Ron Johnson wrote:
> On Wed, Feb 12, 2025 at 2:29 PM Tom Lane <[email-masked] 
> <mailto:[email-masked]>> wrote:

> keys" instead of "use vi keys".

 From man readline:

"By default, the line editing commands are similar to those of emacs.  A
  vi-style line editing interface is also available."

To change from the default would involve setting editing-mode in 
/etc/inputrc or  ~/.inputrc.

---

From: Rob Sargent <[email-masked]>
Date: Wed, 12 Feb 2025 14:17:26 -0700

> keys" instead of "use vi keys".
Or something in yours setting vi?

---

From: Tom Lane <[email-masked]>
Date: Wed, 12 Feb 2025 16:21:21 -0500

> Or something in yours setting vi?

I do have

export EDITOR=emacs

Possibly libreadline is reacting to that, but I've not checked
into it.

			regards, tom lane

---

From: Ben Hancock <[email-masked]>
Date: Wed, 12 Feb 2025 14:09:09 -0800

On 2/12/2025 11:44, Rich Shepard wrote:
> I run psql from a urxvt virtual console, not emacs (which is where I 
> write scripts.)

As with everything in Emacs, there's a mode for that.

In case you're not aware of it, sql-mode allows you not only to interact
with psql from within Emacs, but also to write SQL queries in a buffer
and then send the whole buffer or the region.

It's not documented in the info pages, but from within Emacs, one of
these should get you started:

  C-h P sql
  M-x sql-help

Or just do: M-x sql-postgres

  - Ben

---

From: Vincent Veyron <[email-masked]>
Date: Thu, 13 Feb 2025 18:20:25 +0100

On Wed, 12 Feb 2025 16:21:21 -0500

> Possibly libreadline is reacting to that, but I've not checked

Emacs combinations work on my machine without it.

-- 

                                        Bien à vous, Vincent Veyron 

https://compta.libremen.com
Logiciel libre de comptabilité générale en partie double

---

From: Adrian Klaver <[email-masked]>
Date: Thu, 13 Feb 2025 09:29:52 -0800

> Emacs combinations work on my machine without it.

See my answer here:

https://www.postgresql.org/message-id/4e1966c1-1f24-47e5-9c87-8cf466216b6b%40aklaver.com

for why.

---

From: Ron Johnson <[email-masked]>
Date: Thu, 13 Feb 2025 14:11:15 -0500

> /etc/inputrc or  ~/.inputrc.

It appears that the RHEL default /etc/initrc file defines a bunch of escape
sequences "for linux console and RH/Debian xterm" cursor keys when in emacs
mode.

Our servers are RHEL, my ssh client emulates xterm, and is in the default
emacs mode.  Thus, the cursor keys automagically work for me, and I just
learned that I've been in emacs mode this whole time!

---

From: Rob Sargent <[email-masked]>
Date: Thu, 13 Feb 2025 12:14:10 -0700

On 2/13/25 12:11, Ron Johnson wrote:
> It appears that the RHEL default /etc/initrc file defines a bunch of 
> escape sequences "for linux console and RH/Debian xterm" cursor keys 
> when in emacs mode.
>
> Our servers are RHEL, my ssh client emulates xterm, and is in the 
> default emacs mode.  Thus, the cursor keys automagically work for me, 
> and I just learned that I've been in emacs mode this whole time!
>
> -- 
We're an ancient -not to say insidious- cult.