keyboard shortcuts in psql
there is this quite old, but pretty functional concept: keyboard shortcuts.
you know it - you press ctrl-s, and your editor saves currently-edited file.
my problem is that there are some standard cases when i would really love to have this ability in psql. but psql doesn’t support it. or does it?
luckily psql uses readline. and this gives me ability to do some nice tricks
in my home dir there is (but if there wasn’t, i could just create empty) file named .inputrc.
this is configuration of readline.
and what can be added there? for example this line:
"^[e":"explain analyze "
this string: ^[e should be generated in your editor by pressing ctrl-v and then alt-e.
now. let’s start psql, and press alt-e.
if everything was added correctly you should see “automagically added” explain analyze command
the problem is, that at the moment alt-e works even in shell (you can try it if you want), which doesn’t really break anything, but is not elegant.
luckily - there is solution.
if i will change our mapping to:
$if psql
"^[e":"explain analyze "
$endif
my new mapping will work only in psql.
other usecases might be to issue frequent queries with just a keypress:
"^[OP":"SELECT * FROM pg_stat_activity;^M"
^OP was generated by ctrl-v and then f1 key, and ^M at the end is ctrl-v <enter>
(please note that actual ^xxx sequences on your machine might be different as they depend on terminal).
this will make psql run my select by just pressing f1.
as for other uses - try it yourself. think about what commands do you usually call. perhaps you will want to add shortcuts for “select”, “update”, “insert into”, “delete from”?
only your imagination limits you. anything that can be entered from keyboard can be put in .inputrc thus making your work easier and more time-effective.
April 11th, 2008 at 18:51
[...] On select * from depesz;, Hubert Lubaciewski offers a few tips on some handy, and perhaps little-known keyboard shortcuts in psql. [...]