Toggle Full Path and Folder Name in Ubuntu Terminal Prompt
In Ubuntu, there is no default keyboard shortcut to instantly switch between showing the full working directory path and just the current folder name in your terminal prompt. However, you can achieve this functionality by customizing the Bash shell configuration known as PS1. This article explains how to modify your prompt settings and provides a custom function to toggle between the complete path and the basename efficiently.
The terminal prompt behavior is controlled by the PS1
environment variable. Within this variable, the code \w
displays the full path of the current working directory, while
\W displays only the basename of the current folder. To
change this permanently, you would typically edit your
~/.bashrc file and replace one code with the other.
However, to create a toggle mechanism, you can define a shell function
that swaps these variables on the fly.
To implement the toggle, open your terminal and edit your Bash
configuration file by running nano ~/.bashrc. Scroll to the
bottom of the file and add the following function code. This script
checks the current prompt style and switches it to the alternative
format whenever you run the command.
toggle_path() {
if [[ "$PS1" == *"\\w"* ]]; then
export PS1="${PS1/\\w/\\W}"
echo "Prompt set to folder name only."
else
export PS1="${PS1/\\W/\\w}"
echo "Prompt set to full path."
fi
}After adding the code, save the file and exit the editor. Apply the
changes to your current terminal session by running
source ~/.bashrc. You can now type toggle_path
at any time to switch between viewing the full directory path and just
the folder name in your command prompt. Note that this setting applies
to the working directory display; viewing the full executable path of
commands requires using utilities like which or
type.