83

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.

9 Answers 9

131

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/$

4
  • 18
    This 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
    – spkane
    Nov 29, 2018 at 19:45
  • 2
    Yeah, this is perfect. Jul 3, 2020 at 11:08
  • 1
    @spkane, there's no need to export that to child processes. Just set it as a shell variable, as shown. May 26, 2021 at 12:49
  • This is great, can be also a higher number to see more like PROMPT_DIRTRIM=3. But my question is Is there also the starting section settings? So the result is something like /media/user/..../endDir1/endDir2 ? (specifically without editing PS1 in bashrc or at all directly...)
    – jave.web
    Sep 8, 2023 at 13:26
49

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.

1
  • "if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ ' fi;" the command only applies after the "fi;" to terminate the IF or else becomes incomplete. For me in ubuntu 22.04 "PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$ '" works fine without issues. Mar 25 at 10:05
6

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.

1
  • This worked for WSL 2.0 like a charm. I just put in my bashrc and be done with it.
    – user420792
    Nov 13, 2021 at 20:15
4

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.

2
  • 1
    I have 4 mentions of PS1 Do I change all 4?
    – K Split X
    Jul 22, 2017 at 13:49
  • 4 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.
    – cas
    Jul 22, 2017 at 13:51
2

In zsh prompt strings, %~ expands to the current working directory with things like $HOME changed to ~ or /home/user to ~user like in tcsh, but you can change it to %2~ to limit it to the last 2 components of that path for instance:

$ PS1='%n@%m:%2~$ '
me@here:~/bin$ cd /usr/local/share/man/man1
me@here:man/man1$

Or you can use %20<…<%~%<< to ensure the expansion of %~ is not wider than 20 columns and display that to indicate the truncation if any:

me@here:man/man1$ PS1='%n@%m:%20<…<%~%<<$ '
me@here:…ocal/share/man/man1$
2
  • Very helpful for zsh since PROMPT_DIRTRIM export doesn't work in .zshrc file
    – Arsal
    May 23, 2023 at 19:29
  • @Arsal, yes $PROMPT_DIRTRIM is bash specific, and is also a weird design to choose to have a shell variable to control one prompt expansion (also poor choice to use backslash for prompt expansion which is already way overloaded when csh had been using % for decades beforehand, and bash had already copied many features from csh). In any case, you don't want to export that variable there, that would be unnecessary environment pollution. May 24, 2023 at 6:50
1

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

0

https://github.com/chrissound/SodiumSierraStrawberry

Allows you to truncate a path like:

From: /home/sodium/Projects/Personal/Sierra/Super/Long/Path/HolyAvacado

To: »Projects/Sie…/Sup…/Lon…/Pat…/HolyAvacado/

0
0

just enter:

PS1='\u:\W\$ ' 

and press Enter

1
  • 3
    You 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.
    – Jeff Schaller
    Feb 6, 2022 at 14:59
0

add this to end of bashrc then you type short, it will reduce length on demand :), Thanks @Esmaeil MIRZAEE

short(){
 if [[ $PROMPT_DIRTRIM -eq 0 ]]; then
    PROMPT_DIRTRIM=2
 else
    PROMPT_DIRTRIM=
 fi 
}```

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .