Last Updated: September 09, 2019
·
2.205K
· wordofchristian

fish prompt with rvm and git info

Just started using fish today and I'm really loving it. It took a bit to port my prompt configuration from zsh but was able to finally figure it out:

Picture

put this in your ~/.config/fish/fish.config

function _git_branch_name
  echo (git symbolic-ref HEAD ^/dev/null | sed -e 's|^refs/heads/||')
end

function _is_git_dirty
  echo (git status -s --ignore-submodules=dirty ^/dev/null)
end

function _unpushed
  echo (git cherry -v "@{upstream}" ^/dev/null)
end

function rvm_prompt
  set -l yellow (set_color -o yellow)
  if [ (which rvm) ]
    set -l identifier (rvm tools identifier)
    echo "$yellow$identifier "
  else
    echo ""
  end
end

function fish_prompt
  set -l cyan (set_color cyan)
  set -l yellow (set_color yellow)
  set -l red (set_color -o red)
  set -l blue (set_color blue)
  set -l magenta (set_color -o magenta)
  set -l normal (set_color normal)

  set -l arrow "$red➜"
  set -l cwd $cyan(basename (prompt_pwd))

  if [ (_git_branch_name) ]
    set -l git_branch $red(_git_branch_name)
    set git_info "$blue git:($git_branch$blue)"

    set -l with_unpushed (_git_branch_name)

    if [ (_is_git_dirty) ]
      set arrow "$red✗"
    end

    if [ (_unpushed) ]
      set git_info "$git_info$normal with$magenta unpushed"
    end
  end

  echo -e \n
  echo -n -s  (rvm_prompt) $cwd $git_info
  echo -e \n$arrow $normal
end

Turns out the fish syntax is much nicer than bash. Well worth the effort to migrate.

2 Responses
Add your response

Thank you for posting this, really useful!

I had to change some thing to make it work for me, though...

1) In function rvmprompt I had to change the "if [ (which rvm) ]" to " if type -t rvm >/dev/null" . Since in my system rvm is not an executable in the path but a function (following the instructions at http://rvm.io/integration/fish) so "type" will set the exit code to 0 if the "rvm" exists either as keyword, function, builtin, or file.
2) In function fish
prompt I had to use "if test -n (gitbranchname)" and "if test -n (unpushed)" to make it work in my case

But again thanks for posting this is saved my hours!

over 1 year ago ·

Definitely agree with ecerulm... Also, this might be a bit easier for users. You can create in your ~/.config/fish/ a folder functions and name your functions ie: _git_fish_prompt.fish I then just edited my ~/.config/fish/config.fish to be
function fish_prompt git_fish_prompt end

over 1 year ago ·