Search code examples
macosshellterminalfish

Can I shorten branch names in my terminal in fish


My question is basically this, but for fish, as the solution given there does not apply for this

Currently my fish terminal often looks like

><> ~r/f/d/config on LONG_APP_NAME_RELEASE_CANDIDATE_1_4 x           16:55:12

So it leaves little space for me to actually type in. anyone have any ideas on how to fix this, to maybe look like this:

><> ~r/f/d/config on LONG_A...1_4 x                                   16:55:12

As @glenn proposed in the comments, i typed out type fish_prompt

and got a function

  1 fish_prompt is a function with definition
  2 # Defined in /Users/mge/.config/fish/functions/fish_prompt.fish @ line 5
  3 function fish_prompt
  4   set -l last_command_status $status
  5   set -l cwd
  6 
  7   if test "$theme_short_path" = 'yes'
  8     set cwd (basename (prompt_pwd))
  9   else
 10     set cwd (prompt_pwd)
 11   end
 12 
 13   set -l fish     "⋊>"
 14   set -l ahead    "↑"
 15   set -l behind   "↓"
 16   set -l diverged "⥄ "
 17   set -l dirty    "⨯"
 18   set -l none     "◦"
 19 
 20   set -l normal_color     (set_color normal)
 21   set -l success_color    (set_color $fish_pager_color_progress 2> /dev/null    ; or set_color cyan)
 22   set -l error_color      (set_color $fish_color_error 2> /dev/null; or set_    color red --bold)
 23   set -l directory_color  (set_color $fish_color_quote 2> /dev/null; or set_    color brown)
 24   set -l repository_color (set_color $fish_color_cwd 2> /dev/null; or set_co    lor green)
 25 
 26   if test $last_command_status -eq 0
 27     echo -n -s $success_color $fish $normal_color
 28   else
 29     echo -n -s $error_color $fish $normal_color
 30   end
 31 
 32   if git_is_repo
 33     if test "$theme_short_path" = 'yes'
 34       set root_folder (command git rev-parse --show-toplevel 2> /dev/null)
 35       set parent_root_folder (dirname $root_folder)
 36       set cwd (echo $PWD | sed -e "s|$parent_root_folder/||")
 37     end
 38 
 39     echo -n -s " " $directory_color $cwd $normal_color
 40     echo -n -s " on " $repository_color (git_branch_name) $normal_color " "
 41 
 42     if git_is_touched
 43       echo -n -s $dirty
 44     else
 45       echo -n -s (git_ahead $ahead $behind $diverged $none)
 46     end
 47   else
 48     echo -n -s " " $directory_color $cwd $normal_color
 49   end
 50 
 51   echo -n -s " "
 52 end

This is almost giving me the right idea to solve this, however i don't speak fish so i am not sure how i would edit this


Solution

  • This is the offending line that displays the long branch name

    echo -n -s " on " $repository_color (git_branch_name) $normal_color " "
    

    To shorten that as you request:

    set branch (git_branch_name)
    test (string length $branch) -gt 12
    and set branch (string replace -r '(.{6}).*(.{3})' '$1...$2' $branch)
    
    echo -n -s " on " $repository_color $branch $normal_color " "
    

    I'm not sure what version of fish you need for "string replace" -- if you get errors, you can do

    test (string length $branch) -gt 12
    and set branch (echo $branch | sed -E 's/(.{6}).*(.{3})/\1...\2/')