11

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.

5

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). Log out and in again, or do:

. .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.

  • This was by far the easiest answer to understand. Thanks – K Split X Jul 22 '17 at 14:05
12

Since bash 4, the straightforward way to shorten the depth of directory in command-line is using the below command in your bashrc file. Just remember to reopen your terminal and also the number (i.e. 1) specifies the depth of the directory to show.

PROMPT_DIRTRIM=1

2

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.

  • I have 4 mentions of PS1 Do I change all 4? – K Split X Jul 22 '17 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 '17 at 13:51
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

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.

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/

  • Thanks, but why comment on a 1 year 7 month old post with an already accepted answer? – K Split X Feb 24 at 18:35
  • In the event others find it useful. – Chris Stryczynski Feb 24 at 18:52
  • I really doubt it – K Split X Feb 24 at 21:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.