<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Brett Terpstraprompt - Brett Terpstra</title>
	<atom:link href="http://brettterpstra.com/tag/prompt/feed/" rel="self" type="application/rss+xml" />
	<link>http://brettterpstra.com</link>
	<description>Elegant solutions to complex problems.</description>
	<lastBuildDate>Thu, 09 Feb 2012 15:01:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>My new favorite Bash prompt</title>
		<link>http://brettterpstra.com/my-new-favorite-bash-prompt/</link>
		<comments>http://brettterpstra.com/my-new-favorite-bash-prompt/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 00:12:33 +0000</pubDate>
		<dc:creator>Brett</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[prompt]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://brettterpstra.com/2009/11/17/my-new-favorite-bash-prompt/</guid>
		<description><![CDATA[<p>I do a lot in Terminal. Sometimes, it’s easier. Sometimes it’s faster. Sometimes I’d just rather type it out. Whatever the reason, I’ve never been able to stand looking at a boring shell prompt. Bash is my primary shell, mostly because I’ve never taken the time to learn much else. I’ll get there someday. For now, here’s my current Bash&#8230;</p><p>Originally posted on <a href="http://brettterpstra.com" title="BrettTerpstra.com">BrettTerpstra.com</a> at <a href="http://brettterpstra.com/my-new-favorite-bash-prompt/">My new favorite Bash prompt</a></p>]]></description>
			<content:encoded><![CDATA[<p><img style=' float: left; padding: 4px; margin: 0 7px 2px 0; display:none'  class="alignleft headerimg" src="http://cdn2.brettterpstra.com/wp-content/uploads/2009/11/my_new_favorite_bash_prompt.jpg?9d7bd4" alt="My new favorite Bash prompt" width="440" height="187" />
I do a lot in Terminal. Sometimes, it’s easier. Sometimes it’s faster. Sometimes I’d just rather type it out. Whatever the reason, I’ve never been able to stand looking at a boring shell prompt. Bash is my primary shell, mostly because I’ve never taken the time to learn much else. I’ll get there someday. For now, here’s my current Bash shell prompt…</p>

<p>I’m using the <code>PROMPT_COMMAND</code> variable to run a few quick functions to generate the prompt. It doesn’t do anything processor-intensive, so I haven’t seen any lag caused by this one (unlike some of my previous experiments). <code>PROMPT_COMMAND</code> is set to call a function called, appropriately, <code>prompt_command()</code>. This, in turn, calls a few external functions defined in my <code>.bash_profile</code>. To use it, just stick all of the code below into your <code>.bash_profile</code>, and modify it as you see fit. Be sure to replace any definitions of <code>PROMPT_COMMAND</code> or <code>PS1</code>.</p>

<p><span id="more-163"></span>
The prompt has a few unique features, and some “hidden” features:</p>

<ul>
<li>The current time is formatted just the way I like it. You can modify the <code>fmt_time</code> function with your own <code>strftime</code> strings as desired.</li>
<li>The current 1m average CPU load is included in the prompt in dark grey, gathered using a quick <code>uptime</code> command.</li>
<li>If the previous command returned an error message, the error code returned will show up at the end of the first line in red.</li>
<li>If you use Git, and your current working directory is a Git repository, the current branch will be shown in green before the actual prompt on the second line. This might cause problems if you don’t have Git installed; if you see Git-related errors, you can remove the section of the code under the Git comment from <code>if</code> to <code>fi</code>. Also remove the <code>${BRANCH}</code> from the last line of the <code>prompt_command</code> function.</li>
<li>Lastly, it sets the title of the tab in Terminal to the last two portions of the current working directory (<code>pwd</code>) string, meaning the current directory and its parent directory.</li>
</ul>

<p><img style=' display: block; margin-right: auto; margin-left: auto;'  class="aligncenter size-full wp-image-165" title="TerminalPromptFullMonty" src="http://cdn2.brettterpstra.com/wp-content/uploads/2009/11/TerminalPromptFullMonty.jpg?9d7bd4" alt="TerminalPromptFullMonty" width="536" height="133" /></p>

<p>All of the colors used in the prompt are defined as shell variables. In the final line of the <code>prompt_command</code> function, you can modify the colors just by replacing the color names in the line. That should be pretty self-explanatory.</p>


<div class="wp_syntax"><div class="code"><pre class="bash">prompt_command <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span>
    <span class="kw1">if</span> <span class="br0">&#91;</span> <span class="re4">$?</span> <span class="re5">-eq</span> <span class="nu0">0</span> <span class="br0">&#93;</span>; <span class="kw1">then</span> <span class="co0"># set an error string for the prompt, if applicable</span>
        <span class="re2">ERRPROMPT</span>=<span class="st0">&quot; &quot;</span>
    <span class="kw1">else</span>
        <span class="re2">ERRPROMPT</span>=<span class="st_h">'-&amp;gt;($?) '</span>
    <span class="kw1">fi</span>
    <span class="kw1">if</span> <span class="br0">&#91;</span> <span class="st0">&quot;<span class="es1">\$</span>(type -t __git_ps1)&quot;</span> <span class="br0">&#93;</span>; <span class="kw1">then</span> <span class="co0"># if we're in a Git repo, show current branch</span>
        <span class="re2">BRANCH</span>=<span class="st0">&quot;<span class="es1">\$</span>(__git_ps1 '[ %s ] ')&quot;</span>
    <span class="kw1">fi</span>
    <span class="kw3">local</span> <span class="re2">TIME</span>=<span class="sy0">`</span>fmt_time<span class="sy0">`</span> <span class="co0"># format time for prompt string</span>
    <span class="kw3">local</span> <span class="re2">LOAD</span>=<span class="sy0">`</span><span class="kw2">uptime</span><span class="sy0">|</span><span class="kw2">awk</span> <span class="st_h">'{min=NF-2;print $min}'</span><span class="sy0">`</span>
    <span class="kw3">local</span> <span class="re2">GREEN</span>=<span class="st0">&quot;\[\033[0;32m\]&quot;</span>
    <span class="kw3">local</span> <span class="re2">CYAN</span>=<span class="st0">&quot;\[\033[0;36m\]&quot;</span>
    <span class="kw3">local</span> <span class="re2">BCYAN</span>=<span class="st0">&quot;\[\033[1;36m\]&quot;</span>
    <span class="kw3">local</span> <span class="re2">BLUE</span>=<span class="st0">&quot;\[\033[0;34m\]&quot;</span>
    <span class="kw3">local</span> <span class="re2">GRAY</span>=<span class="st0">&quot;\[\033[0;37m\]&quot;</span>
    <span class="kw3">local</span> <span class="re2">DKGRAY</span>=<span class="st0">&quot;\[\033[1;30m\]&quot;</span>
    <span class="kw3">local</span> <span class="re2">WHITE</span>=<span class="st0">&quot;\[\033[1;37m\]&quot;</span>
    <span class="kw3">local</span> <span class="re2">RED</span>=<span class="st0">&quot;\[\033[0;31m\]&quot;</span>
    <span class="co0"># return color to Terminal setting for text color</span>
    <span class="kw3">local</span> <span class="re2">DEFAULT</span>=<span class="st0">&quot;\[\033[0;39m\]&quot;</span>
    <span class="co0"># set the titlebar to the last 2 fields of pwd</span>
    <span class="kw3">local</span> <span class="re2">TITLEBAR</span>=<span class="st_h">'\[\e]2;`pwdtail`\a'</span>
    <span class="kw3">export</span> <span class="re2">PS1</span>=<span class="st0">&quot;\[<span class="es3">${TITLEBAR}</span>\]<span class="es3">${CYAN}</span>[ <span class="es3">${BCYAN}</span>\u<span class="es3">${GREEN}</span>@<span class="es3">${BCYAN}</span><span class="es1">\
</span>\h<span class="es3">${DKGRAY}</span>(<span class="es3">${LOAD}</span>) <span class="es3">${WHITE}</span><span class="es3">${TIME}</span> <span class="es3">${CYAN}</span>]<span class="es3">${RED}</span><span class="es2">$ERRPROMPT</span><span class="es3">${GRAY}</span><span class="es1">\
</span>\w<span class="es1">\n</span><span class="es3">${GREEN}</span><span class="es3">${BRANCH}</span><span class="es3">${DEFAULT}</span>$ &quot;</span>
<span class="br0">&#125;</span>
<span class="re2">PROMPT_COMMAND</span>=prompt_command
&nbsp;
fmt_time <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="co0">#format time just the way I likes it</span>
    <span class="kw1">if</span> <span class="br0">&#91;</span> <span class="sy0">`</span><span class="kw2">date</span> +<span class="sy0">%</span>p<span class="sy0">`</span> = <span class="st0">&quot;PM&quot;</span> <span class="br0">&#93;</span>; <span class="kw1">then</span>
        <span class="re2">meridiem</span>=<span class="st0">&quot;pm&quot;</span>
    <span class="kw1">else</span>
        <span class="re2">meridiem</span>=<span class="st0">&quot;am&quot;</span>
    <span class="kw1">fi</span>
    <span class="kw2">date</span> +<span class="st0">&quot;%l:%M:%S<span class="es2">$meridiem</span>&quot;</span><span class="sy0">|</span><span class="kw2">sed</span> <span class="st_h">'s/ //g'</span>
<span class="br0">&#125;</span>
pwdtail <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="co0">#returns the last 2 fields of the working directory</span>
    <span class="kw3">pwd</span><span class="sy0">|</span><span class="kw2">awk</span> -F<span class="sy0">/</span> <span class="st_h">'{nlast = NF -1;print $nlast&quot;/&quot;$NF}'</span>
<span class="br0">&#125;</span>
chkload <span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="br0">&#123;</span> <span class="co0">#gets the current 1m avg CPU load</span>
    <span class="kw3">local</span> <span class="re2">CURRLOAD</span>=<span class="sy0">`</span><span class="kw2">uptime</span><span class="sy0">|</span><span class="kw2">awk</span> <span class="st_h">'{print $8}'</span><span class="sy0">`</span>
    <span class="kw1">if</span> <span class="br0">&#91;</span> <span class="st0">&quot;<span class="es2">$CURRLOAD</span>&quot;</span> <span class="sy0">&amp;</span>gt; <span class="st0">&quot;1&quot;</span> <span class="br0">&#93;</span>; <span class="kw1">then</span>
        <span class="kw3">local</span> <span class="re2">OUTP</span>=<span class="st0">&quot;HIGH&quot;</span>
    <span class="kw1">elif</span> <span class="br0">&#91;</span> <span class="st0">&quot;<span class="es2">$CURRLOAD</span>&quot;</span> <span class="sy0">&amp;</span>lt; <span class="st0">&quot;1&quot;</span> <span class="br0">&#93;</span>; <span class="kw1">then</span>
        <span class="kw3">local</span> <span class="re2">OUTP</span>=<span class="st0">&quot;NORMAL&quot;</span>
    <span class="kw1">else</span>
        <span class="kw3">local</span> <span class="re2">OUTP</span>=<span class="st0">&quot;UNKNOWN&quot;</span>
    <span class="kw1">fi</span>
    <span class="kw3">echo</span> <span class="re1">$CURRLOAD</span>
<span class="br0">&#125;</span></pre></div></div>

<p>Related posts:<ol>
<li><a href='http://brettterpstra.com/geeklet-top-cpu-processes/' rel='bookmark' title='Geeklet: Top CPU processes'>Geeklet: Top CPU processes</a></li>
<li><a href='http://brettterpstra.com/oft-bash-function-for-opening-a-specific-filetype/' rel='bookmark' title='oft: Bash function for opening a specific filetype'>oft: Bash function for opening a specific filetype</a></li>
<li><a href='http://brettterpstra.com/na-per-project-todos-in-terminal/' rel='bookmark' title='na: per-project todos in Terminal'>na: per-project todos in Terminal</a></li>
</ol></p><p>Originally posted on <a href="http://brettterpstra.com" title="BrettTerpstra.com">BrettTerpstra.com</a> at <a href="http://brettterpstra.com/my-new-favorite-bash-prompt/">My new favorite Bash prompt</a></p>]]></content:encoded>
			<wfw:commentRss>http://brettterpstra.com/my-new-favorite-bash-prompt/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic (Feed is rejected)
Page Caching using disk: enhanced
Database Caching 8/26 queries in 0.109 seconds using xcache
Object Caching 837/852 objects using xcache
Content Delivery Network via cdn2.brettterpstra.com

Served from: brettterpstra.com @ 2012-02-09 17:25:04 -->
