45

In Bash I have my PS1 as

PS1="\u@\h:\w\$(git branch 2>/dev/null | grep -e '\* ' | sed 's/^..\(.*\)/{\1}/') \$ "

Which will show my current git branch if I am in a git repo.

How do I set the PS1 in fish so it will show me my current git branch?

Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64

4 Answers4

127

@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?

Alexandre Santos
  • 8,170
  • 10
  • 42
  • 64
  • 8
    You actually only need to click "Use prompt" - no copying required! – Ross Hemsley Dec 04 '14 at 09:34
  • 4
    The commands do not show anymore. What is the current way of doing this? – The Unfun Cat Mar 16 '15 at 12:21
  • 1
    @TheUnfunCat If you're using the debian package, you may experience [this issue](https://github.com/fish-shell/fish-shell/issues/1860#issuecomment-68012543). If you see a 404 File not found message in your terminal, create the missing link using: `ln -s /usr/share/javascript/jquery/jquery.js /usr/share/fish/tools/web_config/jquery.js` as root – Arnaud Meuret Dec 08 '15 at 11:52
  • 2
    To find the `fish_prompt` function that this answer winds up creating, I went to `~/.config/fish/functions/`. Note that this is exactly where fish reads the prompt function anyway, so it's now permanent. If you added a git feature (like show current branch) to your prompt, open a new tab and go to a git repo to verify the config is persisted. – Josh Padnick Dec 23 '16 at 23:53
  • 10
    it is now called `Classic + Vcs` as in Version Control System – Vadym Tyemirov Apr 07 '17 at 16:24
  • 1
    Few things have changed now. Now they have "Classic + Git" section – Satish Gadhave Sep 07 '17 at 07:16
  • 1
    Hmm... There is no "Classic + Git" in my latest fish. Also, clicking on one of the available prompts doesn't seem to show any commands. – demisx Apr 27 '19 at 14:08
31

I think this is the equivalent

function fish_prompt
    set -l git_branch (git branch 2>/dev/null | sed -n '/\* /s///p')
    echo -n (whoami)'@'(hostname)':'(prompt_pwd)'{'"$git_branch"'} $ '
end
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
  • [The carret `^` for STDERR redirect will be deprecated in future versions of fish](https://fishshell.com/docs/current/index.html#featureflags) (for future viewers). Currently it works by default. – Spenser Truex Nov 08 '19 at 21:27
  • See [my answer](https://stackoverflow.com/a/58774049/5513492) for the edited version. `s/^/2>/`. – Spenser Truex Nov 08 '19 at 21:34
7

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

Spenser Truex
  • 963
  • 8
  • 24
2

The builtin fish function fish_vcs_prompt generates the prompt for various version control systems, such as git.

Just integrate (fish_vcs_prompt) into your fish prompt:

"\u@\h:\w(fish_vcs_prompt)\$"

The git branch will be written in auto-added brackets with a space in front, for example:

yoda@dagobah:~/git/myRepo (master)$

TheUnseen
  • 314
  • 2
  • 6