How can I use sed to output the current and previous directory while including a tilde if $HOME is one directory further?

StackOverflow https://stackoverflow.com/questions/17032827

Question

Currently my zsh prompt utilizes $'%2~ %%' to output the current and previous directory before just displaying % as my input prompt. For example, if I'm in /Users/david/Documents/Code/project, my prompt will display:

Code/project %

However, if I back up into the Code directory, a tilde is shown:

~Documents/Code %

I'm trying to reproduce this in the fish shell by replacing the regex provided in their prompt_pwd function and gets passed to sed. By default, that function looks like:

function prompt_pwd --description 'Print the current working directory, shortend to fit the prompt'
  echo $PWD | sed -e "s|^$HOME|~|" -e "s|^/private||" -e 's-\([^/]\)[^/]*/-\1/-g'
end

Currently, this outputs the full name of the current directory, but truncates all other directories to one character (and replaces $HOME with a tilde). I'm trying to figure out what regular expression I can provide that function to duplicate what I had going on in zsh.

Was it helpful?

Solution

I suggest:

echo $PWD | sed -e "s|^$HOME/|~|" -e 's-.*/\([^/]*/[^/]*\)-\1/-'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top