If you use the shell in Emacs and you are not already using eshell, convert to eshell now! Eshell is the best terminal emulator available in Emacs. If you need convincing, please read this article from Mastering Emacs. Using grep in eshell is the killer feature for me. What the Master Emacs article didn’t elaborate is how to customize your eshell prompt. I’m going to share how to add colors and show the Git branch on your eshell prompt. This is how my prompt look like currently.
Adding colors
To add colors we need to change the property of the text. We make use of the propertize function. The snippet below changes the string to be green.
You can take a look at the available styles you can apply to text by simply running M-x list-faces-display.
Showing the Git branch name
To get the current branch name from your Git repository, we make use of the function below. I copied this from Jim Mernard’s emacs configuration.
12345678910111213
(defuncurr-dir-git-branch-string(pwd)"Returns current git branch as a string, or the empty string ifPWD is not in a git repo (or the git command is not found)."(interactive)(when(and (eshell-search-path"git")(locate-dominating-filepwd".git"))(let ((git-output(shell-command-to-string(concat"cd "pwd" && git branch | grep '\\*' | sed -e 's/^\\* //'"))))(concat"["(if (> (length git-output)0)(substring git-output0-1)"(no branch)")"]"))))
Installing
I customized Jim’s code to add colors. The end result look like this
(setqeshell-history-size1024)(setqeshell-prompt-regexp"^[^#$]*[#$] ")(load "em-hist"); So the history vars are defined(if (boundp'eshell-save-history-on-exit)(setqeshell-save-history-on-exitt)); Don't ask, just save;(message "eshell-ask-to-save-history is %s" eshell-ask-to-save-history)(if (boundp'eshell-ask-to-save-history)(setqeshell-ask-to-save-history'always)); For older(?) version;(message "eshell-ask-to-save-history is %s" eshell-ask-to-save-history)(defuneshell/ef(fname-regexp&restdir)(effname-regexpdefault-directory));;; ---- path manipulation(defunpwd-repl-home(pwd)(interactive)(let* ((home(expand-file-name(getenv"HOME")))(home-len(length home)))(if (and(>= (length pwd)home-len)(equalhome(substring pwd0home-len)))(concat"~"(substring pwdhome-len))pwd)))(defuncurr-dir-git-branch-string(pwd)"Returns current git branch as a string, or the empty string ifPWD is not in a git repo (or the git command is not found)."(interactive)(when(and (eshell-search-path"git")(locate-dominating-filepwd".git"))(let ((git-output(shell-command-to-string(concat"cd "pwd" && git branch | grep '\\*' | sed -e 's/^\\* //'"))))(propertize(concat"["(if (> (length git-output)0)(substring git-output0-1)"(no branch)")"]")'face`(:foreground"green")))))(setqeshell-prompt-function(lambda ()(concat(propertize((lambda (p-lst)(if (> (length p-lst)3)(concat(mapconcat(lambda (elm)(if (zerop(length elm))""(substring elm01)))(butlastp-lst3)"/")"/"(mapconcat(lambda (elm)elm)(lastp-lst3)"/"))(mapconcat(lambda (elm)elm)p-lst"/")))(split-string(pwd-repl-home(eshell/pwd))"/"))'face`(:foreground"yellow"))(or (curr-dir-git-branch-string(eshell/pwd)))(propertize"# "'face'default))))(setqeshell-highlight-promptnil)
To install, I put the code above in a new file called eshell_customizations.el. Then I loaded the script from my init.el.