PS1 prompt in fish (Friendly Interactive SHell) show git branch

This answer is using the deprecated caret redirect to STDERR. Use 2> instead. Here is the edit

function fish_prompt
    #           Change is here:  vvv
    set -l git_branch (git branch 2>/dev/null | sed -n '/\* /s///p')
    #                            ^^^
    echo -n (whoami)'@'(hostname)':'(prompt_pwd)'{'"$git_branch"'} $ '
end

Here is a coloured prompt I have been using based on the one above:

function fish_prompt
    set_color normal
    # https://stackoverflow.com/questions/24581793/ps1-prompt-in-fish-friendly-interactive-shell-show-git-branch
    set -l git_branch (git branch 2>/dev/null | sed -n '/\* /s///p')
    echo -n (whoami)'@'(hostname)':'
    set_color $fish_color_cwd
    echo -n (prompt_pwd)
    set_color normal
    echo -n '{'
    set_color purple
    echo -n "$git_branch"
    set_color normal
    echo -n '}'
    echo -n ' $ '
end


@glenn already got the answer, but I've found a simpler way of showing the git prompt on fish.

From the terminal, in fish, type fish_config. This will open a browser window. Select the second tab prompt and under there select Classic + Git`.

This will show the commands required to show Git on the terminal prompt. Copy them to your ~/.config/fish/config.fish or even simpler: click on "Use prompt".

How awesome is that?


I think this is the equivalent

function fish_prompt
    set -l git_branch (git branch ^/dev/null | sed -n '/\* /s///p')
    echo -n (whoami)'@'(hostname)':'(prompt_pwd)'{'"$git_branch"'} $ '
end

Tags:

Fish