17

A buddy of mine finally got me to start using Fish Shell and I'm trying to set it up similar to how I had Bash. The PS1 in my .bash_profile listed the current directory I was in, followed by a >. It, however, wasn't the absolute path (e.g. /Users/me/Documents/... or ~/Documents/...). If I was in /Users/me/Documents/projects/Go/project1/, the prompt would simply say project1 >.

Is there a Fish Shell alternative to the \W substitution available for Bash? Again, I just want the folder I'm in, not the full path. I know you can use the echo (pwd) for all that.

I have looked into the basename program, and echo "${PWD##*/}", but these appear to only work in Bash.

4
  • 2
    Can't you use basename as described in the Fish doc? Commented Jul 6, 2015 at 18:02
  • @EugeniuRosca I tried echo (basename) and I get the following: usage: basename string [suffix] basename [-a] [-s suffix] string [...] Commented Jul 6, 2015 at 18:06
  • I have looked into the basename program [...] but these appear to only work in Bash. basename is just a Unix utility; it's not associated to a particular shell, and should work equally well in Bash and Fish. Commented Jul 6, 2015 at 18:06
  • @Jubobs, it looks like I simply didn't format it properly. I ended up using echo (basename $PWD) and this solved it. Commented Jul 6, 2015 at 18:08

4 Answers 4

22

Taken from @Jubobs' answer: basename is just a Unix utility; it's not associated to a particular shell, and should work equally well in Bash and Fish.

It appeared I was using basename in the wrong context, and without a suffix.

This was solved by using the following:

function fish_prompt echo (basename $PWD) "><> " end 
Sign up to request clarification or add additional context in comments.

Comments

6

An alternative: fish ships with a function called prompt_pwd which displays /Users/me/Documents/projects/Go/project1/ as ~/D/p/G/project1

function fish_prompt echo (prompt_pwd) "><> " end 

Comments

1

The complete code of prompt_pwd.fish is below. You must put it inside the directory ~/.config/fish/functions/

function prompt_pwd --description "Print the current working directory, shortened to fit the prompt" set -q argv[1] and switch $argv[1] case -h --help __fish_print_help prompt_pwd return 0 end # This allows overriding fish_prompt_pwd_dir_length from the outside (global or universal) without leaking it set -q fish_prompt_pwd_dir_length or set -l fish_prompt_pwd_dir_length 1 # Replace $HOME with "~" set realhome ~ # @EDITED by Thiago Andrade set tmpdir (basename $PWD) set -l tmp (string replace -r '^'"$realhome"'($|/)' '~$1' $tmpdir) # ORIGINAL VERSION # set -l tmp (string replace -r '^'"$realhome"'($|/)' '~$1' $PWD) if [ $fish_prompt_pwd_dir_length -eq 0 ] echo $tmp else # Shorten to at most $fish_prompt_pwd_dir_length characters per directory string replace -ar '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*/' '$1/' $tmp end end 

Then you'll see something like this

enter image description here

1 Comment

This can be replaced by echo (basename $PWD).
0

This is my function in

~/.config/fish/functions/prompt_pwd.fish

and this seems to work fine

 function prompt_pwd set -q argv[1] and switch $argv[1] case -h --help __fish_print_help prompt_pwd return 0 end set -q fish_prompt_pwd_dir_length or set -l fish_prompt_pwd_dir_length 1 set ttmp $PWD set ttmp (string replace -r '^'$HOME'($|/)' '~$1' $PWD) set -l tmp (basename $ttmp) if [ $fish_prompt_pwd_dir_length -eq 0 ] echo $tmp else string replace -ar '(\.?[^/]{'"$fish_prompt_pwd_dir_length"'})[^/]*/' '$1/' $tmp end end 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.