Personalizar PS1 prompt de forma sencilla

Tanto si sois lo suficientemente vagos como para personalizar el prompt PS1 (prompt string 1) de vuestra línea de comandos como si os resulta tedioso crearlo manualmente esta utilidad online os gustará.

La sintaxis del PS1 es un festival de contra-barras, barras, códigos de color y estilo y muchas otras ordenes más que pueden resultar complejas de generar manualmente. El PS1, para shells en bash se configura en el archivo ~/.bashrc, este sería un ejemplo:

export PS1="\[\e[00;37m\][\$?]\[\e[0m\]\[\e[01;32m\]\u\[\e[0m\]\[\e[00;37m\]@\[\e[0m\]\[\e[01;37m\]\h\[\e[0m\]\[\e[00;37m\]:[\w] \\$\[\e[0m\]"

Semejante línea únicamente para especificar:

  • Exit status code del comando anterior
  • Usuario de la shell en negrita y color verde
  • Hostname en negrita y color blanco
  • PWD en el que nos encontramos
  • Tipo de shell (usuario/superusuario).
[0]alex@hostname:[/usr/local/src] $

En el sitio web bashrcgenerator.com tenéis una interfaz a través de la cual podéis craer un PS1 simplemente arrastrando los distintos elementos y personalizándolos. Podéis añadir y quitar funciones, cambiar colores y estilo, cambiar el orden de los elementos, etc.

bashrc ps1 prompt generator

Podéis usarlo para generar el prompt PS1 base y luego modificarlo manualmente para añadir funcionalidades más avanzadas que ahí no aparezcan. Podemos por ejemplo añadir condiciones, realizar acciones según el resultado del comando anterior, visualizar el número en el que el comando se almacenará en el history, mostrar si hay jobs en background, etc En la página man de bash tenéis el listado de funcionalidades disponibles:

When executing interactively, bash displays the primary prompt PS1 when it is ready to read  a  command,  and the secondary prompt PS2 when it needs more input
to complete a command.  Bash allows these prompt strings to be customized by
inserting a number of backslash-escaped special characters that are decoded as follows:
\a     an ASCII bell character (07)
\d     the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format} the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation.  The braces are required
\e     an ASCII escape character (033)
\h     the hostname up to the first `.'
\H     the hostname
\j     the number of jobs currently managed by the shell
\l     the basename of the shell's terminal device name
\n     newline
\r     carriage return
\s     the name of the shell, the basename of $0 (the portion following the final slash)
\t     the current time in 24-hour HH:MM:SS format
\T     the current time in 12-hour HH:MM:SS format
\@     the current time in 12-hour am/pm format
\A     the current time in 24-hour HH:MM format
\u     the username of the current user
\v     the version of bash (e.g., 2.00)
\V     the release of bash, version + patch level (e.g., 2.00.0)
\w     the  current  working  directory,  with  $HOME  abbreviated with a tilde (uses the value of the PROMPT_DIRTRIM variable)
\W     the basename of the current working directory, with $HOME abbreviated with a tilde
\!     the history number of this command
\#     the command number of this command
\$     if the effective UID is 0, a #, otherwise a $
\nnn   the character corresponding to the octal number nnn
\\     a backslash
\[     begin a sequence of non-printing characters, which could be used to embed  a  terminal  control sequence into the prompt
\]     end a sequence of non-printing characters