order for
the line editing features of <application>Readline</application> to work properly, these
non-printing control characters must be designated as invisible
by surrounding them with <literal>%[</literal> and
<literal>%]</literal>. Multiple pairs of these can occur within
the prompt. For example:
<programlisting>
testdb=> \set PROMPT1 '%[%033[1;33;40m%]%n@%/%R%[%033[0m%]%# '
</programlisting>
results in a boldfaced (<literal>1;</literal>) yellow-on-black
(<literal>33;40</literal>) prompt on VT100-compatible, color-capable
terminals.
</para>
</listitem>
</varlistentry>
<varlistentry id="app-psql-prompting-w">
<term><literal>%w</literal></term>
<listitem>
<para>
Whitespace of the same width as the most recent output of
<varname>PROMPT1</varname>. This can be used as a
<varname>PROMPT2</varname> setting, so that multi-line statements are
aligned with the first line, but there is no visible secondary prompt.
</para>
</listitem>
</varlistentry>
</variablelist>
To insert a percent sign into your prompt, write
<literal>%%</literal>. The default prompts are
<literal>'%/%R%x%# '</literal> for prompts 1 and 2, and
<literal>'>> '</literal> for prompt 3.
</para>
<note>
<para>
This feature was shamelessly plagiarized from
<application>tcsh</application>.
</para>
</note>
</refsect3>
<refsect3 id="app-psql-readline">
<title>Command-Line Editing</title>
<indexterm>
<primary>Readline</primary>
<secondary>in psql</secondary>
</indexterm>
<indexterm>
<primary>libedit</primary>
<secondary>in psql</secondary>
</indexterm>
<para>
<application>psql</application> uses
the <application>Readline</application>
or <application>libedit</application> library, if available, for
convenient line editing and retrieval. The command history is
automatically saved when <application>psql</application> exits and is
reloaded when <application>psql</application> starts up. Type
up-arrow or control-P to retrieve previous lines.
</para>
<para>
You can also use tab completion to fill in partially-typed keywords
and SQL object names in many (by no means all) contexts. For example,
at the start of a command, typing <literal>ins</literal> and pressing
TAB will fill in <literal>insert into </literal>. Then, typing a few
characters of a table or schema name and pressing <literal>TAB</literal>
will fill in the unfinished name, or offer a menu of possible completions
when there's more than one. (Depending on the library in use, you may need to
press <literal>TAB</literal> more than once to get a menu.)
</para>
<para>
Tab completion for SQL object names requires sending queries to the
server to find possible matches. In some contexts this can interfere
with other operations. For example, after <command>BEGIN</command>
it will be too late to issue <command>SET TRANSACTION ISOLATION
LEVEL</command> if a tab-completion query is issued in between.
If you do not want tab completion at all, you
can turn it off permanently by putting this in a file named
<filename>.inputrc</filename> in your home directory:
<programlisting>
$if psql
set disable-completion on
$endif
</programlisting>
(This is not a <application>psql</application> but a
<application>Readline</application> feature. Read its documentation
for further details.)
</para>
<para>
The <option>-n</option> (<option>--no-readline</option>) command line
option can also be useful to disable use
of <application>Readline</application> for a single run
of <application>psql</application>. This prevents tab completion,
use or recording of command line history, and editing of multi-line