From 09bd938e252fd983fb93a50e5c177b4a41642ce0 Mon Sep 17 00:00:00 2001 From: Gary Peck Date: Fri, 9 Oct 2015 15:37:43 -0700 Subject: [PATCH] Add ability to customize the amount of path shortening in prompt_pwd Allows the length of each shortened path component to be customized by setting the `fish_prompt_pwd_dir_length` variable to the number of characters to include (plus a leading dot because that's special). Maintains the default behavior of shortening path components to just one character. You can also set `fish_prompt_pwd_dir_length` to an empty or invalid value or 0 to disable shortening completely. --- share/functions/prompt_pwd.fish | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/share/functions/prompt_pwd.fish b/share/functions/prompt_pwd.fish index 4c766599c..c519c6cc5 100644 --- a/share/functions/prompt_pwd.fish +++ b/share/functions/prompt_pwd.fish @@ -1,4 +1,15 @@ function prompt_pwd --description "Print the current working directory, shortened to fit the prompt" + # 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 ~ - string replace -r '^'"$realhome"'($|/)' '~$1' $PWD | string replace -ar '([^/.])[^/]*/' '$1/' + 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