<?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>Lindesk &#187; prompt</title>
	<atom:link href="http://lindesk.com/tag/prompt/feed/" rel="self" type="application/rss+xml" />
	<link>http://lindesk.com</link>
	<description>Linux - on the Desktop</description>
	<lastBuildDate>Tue, 19 Jan 2010 05:54:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Customizing the Terminal: The Prompt</title>
		<link>http://lindesk.com/2009/03/customizing-the-terminal-the-prompt/</link>
		<comments>http://lindesk.com/2009/03/customizing-the-terminal-the-prompt/#comments</comments>
		<pubDate>Tue, 10 Mar 2009 18:04:13 +0000</pubDate>
		<dc:creator>BinnyVA</dc:creator>
				<category><![CDATA[Command Line]]></category>
		<category><![CDATA[Configuration]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[command]]></category>
		<category><![CDATA[customize]]></category>
		<category><![CDATA[prompt]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[terminal]]></category>

		<guid isPermaLink="false">http://lindesk.com/?p=181</guid>
		<description><![CDATA[Most Linux 'gurus' spend a lot of time working in the terminal. If you belong to that group, this post is for you. This is a tutorial to configure the terminal prompt to the best possible value for your use. Note: This tutorial is for bash users - these instructions will not work in other shells.]]></description>
			<content:encoded><![CDATA[<p><img src="http://lindesk.com/wp-content/uploads/2008/02/terminal.png" alt="Terminal" title="Terminal" width="128" height="128" class="alignnone size-full wp-image-96 intro" align="right" /></p>
<p class="intro">Most Linux &#8216;gurus&#8217; spend a lot of time working in the terminal. If you belong to that group, this post is for you. This is a tutorial to configure the terminal prompt to the best possible value for your use. Note: This tutorial is for bash users &#8211; these instructions will not work in other shells.</p>
<h2>The Prompt</h2>
<p>You must have seen the prompt if you have use the terminal &#8211; it is the first few characters in each line. Usually, it will be&#8230;</p>
<pre><code class="bash">[username@localhost] ~ $</code></pre>
<p>In this case, the user is shown three piece of information in the prompt &#8211; </p>
<ul>
<li>Username of the current user</li>
<li>Hostname</li>
<li>Current folder name</li>
</ul>
<p>This post will show you how to customize this prompt to your needs.</p>
<h3>Editing the Prompt</h3>
<p>Editing the prompt is very simple &#8211; you just have to edit a shell variable. To see the current prompt&#8217;s value, open a shell and type the command&#8230;</p>
<pre><code class="bash">echo $PS1</code></pre>
<p>The result will be something like this(in Ubuntu)&#8230;</p>
<pre><code class="bash">binnyva@binlap:~$ echo $PS1
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\u@\h:\w\$</code></pre>
<p>Which is functionally the same as&#8230;</p>
<pre><code class="bash">\u@\h:\W\$ </code></pre>
<p>To edit this variable, run the command&#8230;</p>
<pre><code class="bash">export PS1=&lt;New Prompt Value&gt;</code></pre>
<p>Most desktop systems don&#8217;t need the username and hostname in the prompt &#8211; this is only relevent if your are connected to a remote system. So the first thing to do, if you are on a desktop system, is to remove those two. To do that, run the command&#8230;</p>
<pre><code class="bash">export PS1="[\W]\$ "</code></pre>
<p>This will change the prompt in the current terminal. To make it permanent, edit the ~/.bashrc and set the PS1 variable there. Just add this line at the end of the file&#8230;</p>
<pre><code class="bash">export PS1="[\W]\$ "</code></pre>
<h2>A Better Prompt</h2>
<p>Currently, the prompt has the basename of the current working directory. That is, if we are in &#8216;~/Sites/Lindesk/posts&#8217;, the prompt will be &#8216;[posts]$ &#8216;. This is good enough for most people. But I have a problem with this. If I go to another folder, say, &#8216;~/Sites/<a href="http://www.openjs.com/">OpenJS</a>/posts&#8217;, the prompt is still &#8216;[posts]$ &#8216;. The prompt is a bit ambiguous in this case. This can be done using a different character &#8211; in this case \w(small &#8216;w&#8217; &#8211; the default was capital &#8216;W&#8217;).</p>
<pre><code class="bash">[posts]$ export PS1="[\w]$ "
[~/Sites/OpenJS/posts]$ _</code></pre>
<p>This is nice &#8211; but you will have a problem if the directory you are in is several levels deep. It might be something like this&#8230;</p>
<pre><code class="bash">[/var/www/html/sites/Lindesk/lindesk.com/wp-content/plugins/eventr/langs]$ _</code></pre>
<p>That&#8217;s long &#8211; and inconvenient. There are better ways of doing this.</p>
<h3>Show the Beginning and the End.</h3>
<p>A better way of doing this is to cut of a part of the folder &#8211; so the above path will look something like&#8230;</p>
<pre><code class="bash">[/var/www/html.../eventr/langs] $ _</code></pre>
<p>This option will show the first 15 characters of the path and then the last 15 characters &#8211; if the directory path is bigger than 30 characters. To enable this mode, open up the file <code>~/.bashrc</code> and add this code&#8230;</p>
<pre><code class="bash">PROMPT_COMMAND='DIR=`pwd|sed -e "s!$HOME!~!"`; if [ ${#DIR} -gt 30 ]; then CurDir=${DIR:0:12}...${DIR:${#DIR}-15}; else CurDir=$DIR; fi'
PS1="[\$CurDir] \$ "</code></pre>
<h3>The First Character of Each Directory</h3>
<p>There is yet another method &#8211; I got this idea from the <a href="http://lindesk.com/2007/04/fishfriendly-interactive-shell/">fish shell</a>. In this approach, the big path will appear as&#8230;</p>
<pre><code class="bash">[/v/w/h/s/L/l/w/p/e/langs] $ _</code></pre>
<p>In this option, only the first character of each parent folder will be shown. Only the base folder name will be shown entirely. This is the approach I use. If you want to use this, open the <code>~/.bashrc</code> file and add this&#8230;</p>
<pre><code class="bash">PROMPT_COMMAND='CurDir=`pwd|sed -e "s!$HOME!~!"|sed -re "s!([^/])[^/]+/!\1/!g"`'
PS1="[\$CurDir] \$ "</code></pre>
<h3>Prompt Variables</h3>
<p>The other values you can insert into the prompt are&#8230;</p>
<dl>
<dt>\d</dt>
<dd>the date in &#8220;Weekday Month Date&#8221; format (e.g., &#8220;Tue May 26&#8243;)</dd>
<dt>\D{format}</dt>
<dd>the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in a locale-specific time representation. The braces are required</dd>
<dt>\e</dt>
<dd>an ASCII escape character (033)</dd>
<dt>\h</dt>
<dd>the hostname up to the first â€˜.â€™</dd>
<dt>\H</dt>
<dd>the hostname</dd>
<dt>\j</dt>
<dd>the number of jobs currently managed by the shell</dd>
<dt>\l</dt>
<dd>the basename of the shellâ€™s terminal device name</dd>
<dt>\n</dt>
<dd>newline</dd>
<dt>\r</dt>
<dd>carriage return</dd>
<dt>\s</dt>
<dd>the name of the shell, the basename of $0 (the portion following the final slash)</dd>
<dt>\t</dt>
<dd>the current time in 24-hour HH:MM:SS format</dd>
<dt>\T</dt>
<dd>the current time in 12-hour HH:MM:SS format</dd>
<dt>\@</dt>
<dd>the current time in 12-hour am/pm format</dd>
<dt>\A</dt>
<dd>the current time in 24-hour HH:MM format</dd>
<dt>\u</dt>
<dd>the username of the current user</dd>
<dt>\v</dt>
<dd>the version of bash (e.g., 2.00)</dd>
<dt>\V</dt>
<dd>the release of bash, version + patch level (e.g., 2.00.0)</dd>
<dt>\w</dt>
<dd>the current working directory, with $HOME abbreviated with a tilde</dd>
<dt>\W</dt>
<dd>the basename of the current working directory, with $HOME abbreviated with a tilde</dd>
<dt>\!</dt>
<dd>the history number of this command</dd>
<dt>\#</dt>
<dd>the command number of this command</dd>
<dt>\$</dt>
<dd>if the effective UID is 0, a #, otherwise a $</dd>
<dt>\nnn</dt>
<dd>the character corresponding to the octal number nnn</dd>
<dt>\\</dt>
<dd>a backslash</dd>
<dt>\[</dt>
<dd>begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt</dd>
<dt>\]</dt>
<dd>end a sequence of non-printing characters</dd>
</dl>
]]></content:encoded>
			<wfw:commentRss>http://lindesk.com/2009/03/customizing-the-terminal-the-prompt/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
	</channel>
</rss>

