How do I shorten the current directory path shown on terminal?
Solution 1
You need to modify PS1
in your shell startup file (probably .bashrc
).
If it's there already, its setting will contain \w
, which is what gives your working directory. Change that to \W
(upper case). The line in bashrc
file looks like below:
if [ "$color_prompt" = yes ]; then
PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '
Log out and in again, or do:
. .bashrc
or (you need to add this prefix '~/' if you are in others directory)
source ~/.bashrc
(or whatever your file is).
If it isn't there, add something like:
PS1='\u@\h: \W:\$'
to .bashrc
or whatever. Look up PS1
in the bash
manual page to get more ideas.
Be careful; bash
can use several more than one initialisation file, e.g. .bashrc
and .bash_profile
; it may be that PS1
is set in a system-wide one. But you can override that in one of your own files.
Solution 2
Since bash 4, to shorten the depth of directory in command-line is done by using PROMPT_DIRTRIM
in the .bashrc
file. Just remember to reopen your terminal.
PROMPT_DIRTRIM=1
See the Bash Manual for more information.
Example
bob@bob-ubuntu:~/Desktop/Dropbox/School/2017/C/A3/$
will be trimmed to
bob@bob-ubuntu:.../A3/$
Solution 3
Assuming you're using bash, change the prompt string (variable PS1) so that it has \W
instead of \w
.
e.g. if your PS1 is currently \u@\h:\w\$
, set it to \u@\h:\W\$
To make this permanent, you will have to change it in your bash startup files - e.g. ~/.bash_profile
or ~/.bashrc
.
see man bash
and search for PROMPTING
for full details and a list of backslash-escaped special characters.
Solution 4
This is portable to all sh
shells.
Assign to PS1
in one of your shell startup files:
PS1='${PWD##*/} $ '
The prompt will look like
dir $
Where dir
is the base name of the current directory.
The $PWD
variable contains the current directory path, and ${PWD##*/}
will strip the everything up to and including the last /
in that path.
The single quotes prevents the shell form evaluating the variable substitution at the time of assignment (the value of $PS1
will be evaluated each time the prompt is displayed).
The PS1
variable should not be exported as it's only used by the current shell.
Solution 5
in this case you will have to edit PS1 ,
insted of \w
, you will have a command or a variable that shows shortned path :
original PS1
PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$'
change it to
PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]${PWD##*/}\[\033[00m\]\$'
Note this will put the username insteed of ~ if you are in your home dir !
to avoid that you will need a few commands insteed of ${PWD##*/} e.g.
if [[ "${PWD}" == "${HOME}" ]] ; then printf \~; else echo -n ${PWD##*/}; fi
the new PS1 will look like the following
PS1='\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]`if [[ "${PWD}" == "${HOME}" ]] ; then printf \~; else echo -n ${PWD##*/}; fi`\[\033[00m\]\$'
oOps while i am trying to save the world i had noIdea|forgoten the \W
Related videos on Youtube
![K Split X](https://i.stack.imgur.com/GxhO2.png?s=256&g=1)
K Split X
Updated on September 18, 2022Comments
-
K Split X almost 2 years
If I am in a deep directory, let's say:
~/Desktop/Dropbox/School/2017/C/A3/
then when I open up terminal, it says
bob@bob-ubuntu:~/Desktop/Dropbox/School/2017/C/A3/$
and then I write my command. That is very long, and every line I write in the terminal goes to the next line. I want to know if there's a way so that it only displays my current directory. I want it to display:
bob@bob-ubuntu: A3/$
This way it's much clear, and always I can do
pwd
to see my entire directory. I just don't want the entire directory visible in terminal because it takes too much space. -
K Split X almost 7 yearsI have 4 mentions of PS1 Do I change all 4?
-
Alessio almost 7 years4 mentions where? in ~/.bash_profile? it should do no harm to change all instances of
\w
in PS1 to\W
. or you could just set the prompt to whatever you like at the bottom of the script. -
spkane over 5 yearsThis is EXACTLY what I was looking for. Setting something like
export PROMPT_DIRTRIM=3
in your.bashrc
is the perfect middle ground between\W
and\w
. See: gnu.org/software/bash/manual/html_node/Bash-Variables.html -
Lonnie Best almost 4 yearsYeah, this is perfect.
-
Toby Speight about 3 years@spkane, there's no need to
export
that to child processes. Just set it as a shell variable, as shown. -
Admin over 2 yearsThis worked for WSL 2.0 like a charm. I just put in my bashrc and be done with it.
-
Jeff Schaller over 2 yearsYou might explain that this setting will go away each time they exit the shell, and consider improving your post to describe how to set it permanently. You could also describe what the syntax is doing, exactly.