El título de la ventana del terminal se puede cambiar cambiando el valor de la variable $PS1: la cadena de solicitud primaria. [1] [2] . Podríamos combinar esta solución con la idea de usar el comando de la respuesta de Dessert . history
Enfoque 1: actualice el valor de forma $PS1automática. (Actualizar)
Agregue las siguientes líneas al final del archivo ~/.bashrc:
# Change the terminal window title, based on the last executed command
rtitle() {
# If the variable $PS1_bak is unset,
# then store the original value of $PS1 in $PS1_bak and chang $PS1
# else restore the value of $PS1 and unset @PS1_bak
if [ -z "${PS1_bak}" ]; then
PS1_bak=$PS1
PS1+='\e]2;$(history 1 | sed "s/^[0-9 ]* \+//")\a'
else
PS1=$PS1_bak
unset PS1_bak
fi
};
export -f rtitle # Export the function to be accessible in sub shells
#rtitle # Uncomment this line to change the default behaviour
Luego, source ~/.bashrco simplemente abra una nueva terminal y use la función de esta manera:
- Ejecute
rtitlepara comenzar a cambiar el título de la ventana del terminal automáticamente, según el último comando ejecutado.
- Ejecute
rtitleuna vez más para volver al comportamiento predeterminado.
Enfoque 2: actualice el valor de forma $PS1manual. (Respuesta inicial)
Agregue las siguientes líneas al final del archivo ~/.bashrc:
set-title() { # Set a title of the current terminal window
[[ -z ${@} ]] && TITLE="$(history 2 | head -1 | sed "s/^[0-9 ]* \+//")" || TITLE="$@" # If the title is not provided use the previous command
[[ -z ${PS_ORIGINAL} ]] && PS_ORIGINAL="${PS1}" || PS_ORIGINAL="${PS_ORIGINAL}" # Use the original value of PS1 for each future change
PS1="${PS_ORIGINAL}"'\e]2;'"$TITLE"'\a' # Change the prompt (the value of PS1)
}; export -f set-title
Luego, source ~/.bashrco simplemente abra una nueva terminal y use la función de esta manera:
set-title <something>cambiará el título de la ventana de terminal a <something>.
set-title sin argumento cambiará el título de la ventana de terminal al comando anterior.
Referencias y ejemplos: