Toggle format of gnome terminal prompt string by command?
My terminal has a default prompt format like this one:
username@boxname /path/to/current/directory $
The code that produces it looks like this: (it has some color definitions too)
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\]'
If the path to the current directory gets too long it becomes unpleasant to work with the terminal because you constantly break lines. In such cases I would prefer a format that produces a shorter string like this one:
username@boxname current_dir_name $
The code that produces it would look like this (again with color):
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] $(basename ${PWD}) \$ \[\033[00m\]'
Does anyone know how I could easily toggle the format of the current terminal window from one style to the other by just typing for example: prompttoggle
?
Store both your long and short PS1
variables under a different name:
PS1short='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\]'
PS1long='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] $(basename ${PWD}) \$ \[\033[00m\]'
Make sure to set PS1
to one of them initially:
PS1="$PS1long"
Then you can make an alias like this to toggle between the two PS1 values:
alias prompttoggle='if test "$PS1" = "$PS1long" ; then PS1="$PS1short" ; else PS1="$PS1long" ; fi'
Adding all four lines to your ~/.bashrc
file will ake the command available in your Bash sessions, here are they again for easier copying:
PS1short='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\]'
PS1long='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] $(basename ${PWD}) \$ \[\033[00m\]'
PS1="$PS1long"
alias prompttoggle='if test "$PS1" = "$PS1long" ; then PS1="$PS1short" ; else PS1="$PS1long" ; fi'
You can use a tiny bash
function:
prompttoggle () {
if [[ $PS1 = *basename* ]]; then
export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\]'
else
export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] $(basename ${PWD}) \$ \[\033[00m\]'
fi ;}
The function above matches if the current $PS1
contains basename
, if yes, then the PS1
without basename
is set otherwise the one with basename
is set.
Put the function in your ~/.bashrc
to get it available in all interactive sessions.
Example:
foo@bar:~/spam/egg$ prompttoggle () {
> if [[ $PS1 = *basename* ]]; then
> export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] \w \$\[\033[00m\]'
> else
> export PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;34m\] $(basename ${PWD}) \$ \[\033[00m\]'
> fi ;}
foo@bar:~/spam/egg$ prompttoggle
foo@bar egg $ prompttoggle
foo@bar ~/spam/egg $
Or... make two very tiny functions and add to the end of your ~/.bashrc
# short prompt
promptshort() { export PS1=$(echo "$PS1" | sed 's/\\w/\\W/g') ; }
# full length prompt
promptlong() { export PS1=$(echo "$PS1" | sed 's/\\W/\\w/g') ; }
for a shortened prompt, type promptshort
, to go back to the full path, type promptlong
\W
shows the current working directory only so $(basename ${PWD})
is overkill imho
You could combine into one function but "long" and "short" are descriptive and both have less keystrokes than "toggle" ;)
Instead of adding a line to override PS1 I prefer to tweak the code that sets it (for example, uncomment force_color_prompt=yes
and edit the line after [ "$color_prompt" = yes ]; then
)
Here's a ~/.bashrc
function definition that I personally use to reset/toggle my prompt from regular prompt to just $
. Adapt it as necessary to suit your needs.
resetps() {
if [ "$PS1" = "$ " ]
then
PS1=$OLDPS1
else
OLDPS1=$PS1
export OLDPS1
PS1="$ "
fi
}