如何在 Emacsshell
提示符中显示当前 git 分支?
例如,我的~/.bash_profile
(我在 Mac OSX 10.9 终端上)中有这个:
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
export PS1="\[\033[00m\]\u@\h\[\033[01;33m\] \w \[\033[31m\]\$(parse_git_branch)\[\033[00m\]$\[\033[00m\] "
它会产生一个颜色鲜艳的 bash shell,如下所示:
name@my-computer ~/code/sample (master)$
我怎样才能在 emacs shell 中生成类似的东西?
目前,当我打开它时,M-x shell
它将像这样打开:
bash: parse_git_branch: command not found
name@my-computer ~/code/sample $
答案1
遇到了同样的问题,经过一番研究发现这repo 并采用我的代码到以下解决方案:
获取分支名称:
(defun git-prompt-branch-name ()
"Get current git branch name"
(let ((args '("symbolic-ref" "HEAD" "--short")))
(with-temp-buffer
(apply #'process-file "git" nil (list t nil) nil args)
(unless (bobp)
(goto-char (point-min))
(buffer-substring-no-properties (point) (line-end-position))))))
自定义提示功能:
(defun 4lex1v:eshell-prompt ()
(let ((branch-name (git-prompt-branch-name)))
(concat
"\n# " (user-login-name) " in " (abbreviate-file-name (eshell/pwd)) "\n"
(if branch-name (format "git:(%s) >> " branch-name) ">> ")
)))
配置:
(setq eshell-prompt-function #'4lex1v:eshell-prompt
eshell-prompt-regexp ".*>>+ ")