add custom prompts with PS1 and PS2

PS1 and PS2 support the backslash escapes of bash (\u \h \w \W \$ \t \j
\e \[ \] ...) and expand $VAR, ${...}, $(...) and $((...)) before each
prompt. Values of escapes and expansions are sanitized. Lines before the
last one and non-SGR escape sequences are written before readline's
prompt. Without PS1, the default prompt is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Florian WaltherandClaude Opus 5.5 committed 2026-10-05 22:49:19 +02:00
1 parent af5281eed2
commit 82f9c90a39
6 files changed
+528 -13

No files matched your search

+66 -3
View File
@@ -445,7 +445,9 @@ and 2 are supported.
.PP
Control structures may span several lines; an incomplete command shows
the continuation prompt
.BR "> " .
.B $PS2
(default
.BR "> " ).
.B !
negates the exit status of a command.
.B for
@@ -731,7 +733,9 @@ Exit the shell.
.PP
Lines starting with a space are not saved in the history.
.PP
The prompt shows
If
.B PS1
is not set, the prompt shows
.IR user @ host : dir
followed by
.BR $ .
@@ -746,6 +750,55 @@ the OSC 7 escape sequence
so terminals like GNOME Terminal open new tabs and windows in the same
directory. Nothing is sent if the output is not a terminal or
.BR TERM=dumb .
.SS Custom prompts
.B PS1
and
.B PS2
are expanded before each prompt. They may contain the backslash escapes
of
.BR bash (1):
.PP
.RS
.nf
\fB\e\&u\fP user name
\fB\e\&h\fP, \fB\e\&H\fP host name up to the first \(oq.\(cq, full host name
\fB\e\&w\fP, \fB\e\&W\fP working directory (\fB~\fP for $HOME), its last component
\fB\e\&$\fP \fB#\fP for root, otherwise \fB$\fP
\fB\e\&t\fP, \fB\e\&T\fP time as HH:MM:SS (24 and 12 hours)
\fB\e\&@\fP, \fB\e\&A\fP time as HH:MM AM/PM and HH:MM (24 hours)
\fB\e\&d\fP date, e.g. Mon Oct 05
\fB\e\&j\fP number of jobs
\fB\e\&l\fP terminal name
\fB\e\&s\fP, \fB\e\&v\fP, \fB\e\&V\fP shell name, version (major.minor), full version
\fB\e\&n\fP, \fB\e\&r\fP newline, carriage return
\fB\e\&a\fP, \fB\e\&e\fP bell, escape
\fB\e\&\fP\fInnn\fP character with octal code \fInnn\fP
\fB\e\e\fP backslash
\fB\e[\fP, \fB\e]\fP ignored (needed by bash around escape sequences)
.fi
.RE
.PP
Unknown escapes are kept as they are. Then
.BI $ VAR\fR,\fP
.BR ${...} ,
.B $(...)
and
.B $((...))
are expanded;
.B $?
is the status of the last command. Control characters in the values of
escapes and expansions are shown escaped, so colors must be written in
the prompt itself, e.g.
.PP
.RS
.nf
PS1=\(aq\e[\ee[1;32m\e]\eu@\eh\e[\ee[0m\e]:\ew\e$ \(aq
.fi
.RE
.PP
If the prompt cannot be expanded (e.g. because of an unset variable), an
error is printed and the default prompt is used. Escape sequences other
than colors (e.g. the window title) are written before the prompt.
.SH ENVIRONMENT
.TP
.B GOSH_HISTFILE
@@ -755,8 +808,18 @@ Path of the history file. If set to an empty value, no history is saved.
Base directory of the default history file
.IR $XDG_STATE_HOME/gosh/history .
.TP
.B PS1
Prompt of the interactive shell, see
.BR "Custom prompts" .
.TP
.B PS2
Prompt for continuation lines (default
.BR "> " ).
.TP
.B GOSH_COLORS
Prompt colors as colon-separated
Colors of the default prompt (if
.B PS1
is not set) as colon-separated
.IB key = code
entries. Keys are
.B user