在Bash
中,PS1
为
PS1="u@h:w$(git branch 2>/dev/null | grep -e '* ' | sed 's/^..(.*)/{1}/') $ "
这将显示我当前的git branch
如果我在一个git的回购。
我如何在fish
中设置PS1
,以便它会显示我当前的git分支?
@glenn已经得到了答案,但是我发现了一种更简单的方法来显示fish上的git提示符。
在fish终端输入fish_config
。这将打开一个浏览器窗口。选择第二个选项卡prompt
,在那里选择Classic + Git ' .
这将显示在终端提示符上显示Git所需的命令。把它们复制到你的~/.config/fish/config.fish
,或者更简单:点击"使用提示符"。
这有多棒啊?
我认为这是等效的
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
这个答案使用了已弃用的插入符号重定向到STDERR。使用2>
代替。下面是编辑
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
这是我在上面的基础上使用的彩色提示符:
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
内置的fish函数fish_vcs_prompt
生成各种版本控制系统(如git)的提示符。
将(fish_vcs_prompt)
集成到fish提示符中:
"u@h:w(fish_vcs_prompt)$"
git分支将被写在自动添加的括号中,前面加一个空格,例如:
yoda@dagobah: ~/git/myRepo(主)$