3

I'm currently working in a really long directory. Probably something like:

~/Documents/c_prog/this_is_a_really_long_project_name/sub_directory/test_case/

It would be awesome if I could execute a command when I'm in this directory and it would only show *sername@hostname ~/test_case$ instead of this wall of text. When I restart bash it should just be normal again. I'm totally fine with executing this command every time I'm in this directory.

How can I achieve something like this?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
  • Try setting `PROMPT_DIRTRIM=1`. It's not exactly what you want, but shortens the directory considerable. – chepner Nov 13 '15 at 17:33

2 Answers2

5

Replace the \w escape sequence in your normal PS1 with \W. Thus, from your example, it would be something like:

PS1='\u@\h \W\$ '
Tom Hunt
  • 9,808
  • 4
  • 25
  • 43
  • That only replace `/home/user` with `~`; it affect the rest of the path. – chepner Nov 13 '15 at 17:34
  • No, `\W` shows the basename of the current directory, replacing it with `~` only if the CWD is the homedir. Read `man bash`. – Tom Hunt Nov 13 '15 at 17:35
  • I did, but misread and misremembered. Sorry. – chepner Nov 13 '15 at 17:37
  • This is why I love stackexchange. 5 minutes in and already got a quick solution! – LastSecondsToLive Nov 13 '15 at 17:54
  • I tried to put it into a bash script: `#!/bin/bash export PS1=">"` but it doesn't seem to change anything. What am I doing wrong? I added the path the file is located in to my PATH in `.bashrc_profile` and made it executable. I can tell the script is being executed by adding an echo`but somehow PS1 doesn't change. – LastSecondsToLive Nov 13 '15 at 18:30
  • When you alter the environment in a bash script, the change doesn't propagate to your currently running shell. Type it by hand, or type `source path/to/script.sh`. – Tom Hunt Nov 13 '15 at 18:40
2

(bash 4 or later)

If you set PROMPT_DIRTRIM to a non-zero number, it will replace directories after ~ with ..., retaining the given number of trailing directories. Setting it to 1, for example, will give you

username@hostname ~/.../test_case$
chepner
  • 7,341
  • 1
  • 26
  • 27
  • Thank you really much! Do I have to put it in my `.bashrc`. It somehow doesn't seem to work if I just write it in the command-line. – LastSecondsToLive Nov 13 '15 at 17:55
  • I forgot to check before posting; `PROMPT_DIRTRIM` was introduced in `bash` 4; you appear to be using an earlier version. – chepner Nov 13 '15 at 17:58
  • That is quite possible. I'm actually on a Mac at the moment. I'll check right away :) Thanks again for the help! – LastSecondsToLive Nov 13 '15 at 18:14
  • Mac OS X still ships with `bash` 3.2. – chepner Nov 13 '15 at 18:28
  • Yeah, I just saw it as well. I tried to write a small bash script to update my $PS1 to something with `\W`(like the answer above mentioned). But somehow my PS1 isn't updating.. Do you have any idea why? The script looks like this: `#!/bin/bash echo This is a test! export PS1=">"` The Test phrase is printed, but the variable doesn't change... – LastSecondsToLive Nov 13 '15 at 18:36
  • Oh, I just read that export doesn't affect subshells. – LastSecondsToLive Nov 13 '15 at 18:39
  • Generally, you don't need to export `PS1`; only `bash` uses it, and it's generally set from `.bashrc` when `bash` starts up. – chepner Nov 13 '15 at 19:31