2

How can I show the current git branch as part of the Emacs shell prompt?

For example, I have this in my ~/.bash_profile (I'm on Mac OSX 10.9 Terminal):

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\] "

which produces a nicely colored bash shell like this:

name@my-computer ~/code/sample (master)$

How can I produce something similar in the emacs shell?

Currently when I open M-x shell it will open like so:

bash: parse_git_branch: command not found
name@my-computer ~/code/sample $ 

1 Answer 1

0

Had the same issue, after some research found this repo and adopted my code to the following solution:

Get branch name:

  (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))))))

Custom prompt function:

  (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) ">> ")
       )))

Configure:

  (setq eshell-prompt-function #'4lex1v:eshell-prompt
        eshell-prompt-regexp ".*>>+ ")

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .