161 lines
No EOL
8.1 KiB
HTML
161 lines
No EOL
8.1 KiB
HTML
<!DOCTYPE html>
|
|
<html lang='en'><head><meta charset='utf-8' /><meta name='pinterest' content='nopin' /><link href='../../../../static/css/style.css' rel='stylesheet' type='text/css' /><link href='../../../../static/css/print.css' rel='stylesheet' type='text/css' media='print' /><title>Mercurial Bash Prompts / Steve Losh</title></head><body><header><a id='logo' href='https://stevelosh.com/'>Steve Losh</a><nav><a href='../../../index.html'>Blog</a> - <a href='https://stevelosh.com/projects/'>Projects</a> - <a href='https://stevelosh.com/photography/'>Photography</a> - <a href='https://stevelosh.com/links/'>Links</a> - <a href='https://stevelosh.com/rss.xml'>Feed</a></nav></header><hr class='main-separator' /><main id='page-blog-entry'><article><h1><a href='../../../entry/2009/3/17/mercurial-bash-prompts/index.html'>Mercurial Bash Prompts</a></h1><p class='date'>Posted on March 17th, 2009.</p><p>I've been spending a lot of time in the Terminal lately. I use bash, and it
|
|
lets you configure the prompt pretty much however you want. I won't go into
|
|
how to do the most basic configuration here - if you want to get up to speed
|
|
check out this <a href="http://www.ibm.com/developerworks/linux/library/l-tip-prompt/">guide</a>.</p>
|
|
|
|
<p>In this post I'm just going to talk about one simple addition that I
|
|
personally find very helpful: displaying the current branch of a <a href="http://mercurial-scm.org/">Mercurial</a>
|
|
repository.</p>
|
|
|
|
<h2>Update: the hg-prompt Extension</h2>
|
|
|
|
<p>I swear, this is the last time I'm updating this entry. I went ahead and made
|
|
an extension for Mercurial called
|
|
<a href="http://stevelosh.com/projects/hg-prompt/">hg-prompt</a> that does everything
|
|
much more elegantly. <strong>Use that instead!</strong></p>
|
|
|
|
<h2>My Starting Point</h2>
|
|
|
|
<p>Here's what my prompt looked like a couple of days ago:</p>
|
|
|
|
<p><img src="../../../../static/images/blog/2009/03/prompt-without-branch.png" alt="My bash prompt without the branch displayed" title="My bash prompt without the branch displayed."></p>
|
|
|
|
<p>Here's the code in my <code>.bashrc</code> file to create it. I've stripped out the color
|
|
information to save space.</p>
|
|
|
|
<pre><code>export PS1='\n\u at \h in \w\n$ '</code></pre>
|
|
|
|
<p>I use the same prompt on every computer I work with, so with this prompt I can
|
|
see which user I'm logged in as, which machine I'm on, and where in the
|
|
filesystem I am. It's a lot of useful information at a glance but doesn't seem
|
|
too cluttered.</p>
|
|
|
|
<p>I have a linebreak in there because the filesystem path can get pretty long.
|
|
If I kept it all on one line most of my commands would be wrapped around
|
|
anyway, which I find harder to read.</p>
|
|
|
|
<h2>Adding the Mercurial Branch</h2>
|
|
|
|
<p>I use Mercurial for version control, and I use branches quite a bit when I'm
|
|
developing. One problem with this is that it's easy to forget which branch I'm
|
|
on at a given moment. I <em>could</em> just use <code>hg branch</code> to find out, but that
|
|
gets tedious to type, even when aliased to something like <code>hb</code>.</p>
|
|
|
|
<p>A few days ago I got the idea that I could just display the current branch on
|
|
my prompt whenever I'm in a directory that's part of a repository. Here's what
|
|
my prompt looks like now:</p>
|
|
|
|
<p><img src="../../../../static/images/blog/2009/03/prompt-with-branch.png" alt="My bash prompt with the branch displayed" title="My bash prompt with the branch displayed."></p>
|
|
|
|
<p>And here's the code in my <code>.bashrc</code> that does it:</p>
|
|
|
|
<pre><code>hg_in_repo() {
|
|
hg branch 2> /dev/null | awk '{print "on "}'
|
|
}
|
|
|
|
hg_branch() {
|
|
hg branch 2> /dev/null | awk '{print $1}'
|
|
}
|
|
|
|
export PS1='\n\u at \h in \w $(hg_in_repo)$(hg_branch)\n$ '</code></pre>
|
|
|
|
<p>The <code>on branchname</code> piece is only displayed when you're in a directory that's
|
|
part of a Mercurial repository.</p>
|
|
|
|
<p>I've split it up into two separate functions because I wanted to have <code>on</code> and
|
|
<code>branchname</code> displayed in two different colors. I couldn't seem to include the
|
|
color codes in the awk command, so I split it up and put the colors in the
|
|
export statement with the rest of them. If you don't care about colors (or
|
|
don't mind having both words the same color) you can just collapse it into one
|
|
function.</p>
|
|
|
|
<h2>Updated: Is It Dirty?</h2>
|
|
|
|
<p>After I posted this entry Matt Kemp commented with a link to a <a href="http://gist.github.com/31631">git
|
|
version</a>. One feature that version has is a simple indicator of whether or
|
|
not the repository you're in is dirty. I ported it to Mercurial and here's the
|
|
result:</p>
|
|
|
|
<p><img src="../../../../static/images/blog/2009/03/prompt-with-dirty.png" alt="My bash prompt with the branch and dirty indicator displayed" title="My bash prompt with the branch and dirty indicator displayed."></p>
|
|
|
|
<p>And the code in <code>.bashrc</code>:</p>
|
|
|
|
<pre><code>hg_dirty() {
|
|
hg status --no-color 2> /dev/null \
|
|
| awk '$1 == "?" { print "?" } $1 != "?" { print "!" }' \
|
|
| sort | uniq | head -c1
|
|
}
|
|
|
|
hg_in_repo() {
|
|
[[ `hg branch 2> /dev/null` ]] && echo 'on '
|
|
}
|
|
|
|
hg_branch() {
|
|
hg branch 2> /dev/null
|
|
}
|
|
|
|
export PS1='\n\u at \h in \w $(hg_in_repo)$(hg_branch)$(hg_dirty)\n$ '</code></pre>
|
|
|
|
<p>This gives you a <code>?</code> after the branch when there are untracked files (and
|
|
<em>only</em> untracked files), and a <code>!</code> if there are any modified, tracked files.</p>
|
|
|
|
<h2>Updated: Bookmarks Too!</h2>
|
|
|
|
<p>I've added another piece to show bookmarks as well. I've also figured out how
|
|
to add colors directly in the functions, so here's the (much nicer) updated
|
|
code all at once:</p>
|
|
|
|
<pre><code>DEFAULT="[37;40m"
|
|
PINK="[35;40m"
|
|
GREEN="[32;40m"
|
|
ORANGE="[33;40m"
|
|
|
|
hg_dirty() {
|
|
hg status --no-color 2> /dev/null \
|
|
| awk '$1 == "?" { unknown = 1 }
|
|
$1 != "?" { changed = 1 }
|
|
END {
|
|
if (changed) printf "!"
|
|
else if (unknown) printf "?"
|
|
}'
|
|
}
|
|
|
|
hg_branch() {
|
|
hg branch 2> /dev/null | \
|
|
awk '{ printf "\033[37;0m on \033[35;40m" $1 }'
|
|
hg bookmarks 2> /dev/null | \
|
|
awk '/\*/ { printf "\033[37;0m at \033[33;40m" $2 }'
|
|
}
|
|
|
|
export PS1='\n\e${PINK}\u \
|
|
\e${DEFAULT}at \e${ORANGE}\h \
|
|
\e${DEFAULT}in \e${GREEN}\w\
|
|
$(hg_branch)\e${GREEN}$(hg_dirty)\
|
|
\e${DEFAULT}\n$ '</code></pre>
|
|
|
|
<p>These are some pretty simple changes but they help keep me sane. One thing to
|
|
be aware of: if you use all of these it does slow down the rendering of the
|
|
prompt by a tiny, but noticeable, amount. I'm not the strongest bash scripter,
|
|
so if there's a better way to do this (or a way that will make it faster and
|
|
reduce the delay) please let me know!</p>
|
|
|
|
<p><strong>UPDATE:</strong> Matt Kemp posted a link to a <a href="http://gist.github.com/31631">git version</a> of this below. If you
|
|
use git, check it out! One thing that version has that I didn't think of is an
|
|
indicator of whether the repository is dirty (has uncommitted changes). I'm
|
|
going to go ahead and steal that idea for my prompt too.</p>
|
|
|
|
<p><strong>UPDATE:</strong> By request, I've written <a href="../../../entry/2009/3/18/candy-colored-terminal/index.html">an entry about the
|
|
colors</a>.</p>
|
|
|
|
<p><strong>UPDATE:</strong> Kevin Bullock pointed out that the Python interpreter needed to be
|
|
started a bunch of times which will degrade performance. I've changed up the
|
|
"dirty" code a bit to reduce the number of interpreters needed. It's still not
|
|
as efficient as his version, but I think it's about as good as I'm going to
|
|
get if I want separate colors for the pieces and don't want to rely on an
|
|
external script.</p>
|
|
|
|
<p><strong>FINAL UPDATE:</strong> I made an extension for Mercurial called
|
|
<a href="http://stevelosh.com/projects/hg-prompt/">hg-prompt</a> that does everything
|
|
much more elegantly. <strong>Use that instead!</strong></p>
|
|
</article></main><hr class='main-separator' /><footer><nav><a href='https://github.com/sjl/'>GitHub</a> ・ <a href='https://twitter.com/stevelosh/'>Twitter</a> ・ <a href='https://instagram.com/thirtytwobirds/'>Instagram</a> ・ <a href='https://hg.stevelosh.com/.plan/'>.plan</a></nav></footer></body></html> |