<?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>Push cx &#187; xterm</title>
	<atom:link href="http://push.cx/tag/xterm/feed" rel="self" type="application/rss+xml" />
	<link>http://push.cx</link>
	<description>A traveling geek&#039;s blog on development, games, and the web</description>
	<lastBuildDate>Thu, 19 Apr 2012 20:39:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>256-Color XTerms in Ubuntu</title>
		<link>http://push.cx/2008/256-color-xterms-in-ubuntu</link>
		<comments>http://push.cx/2008/256-color-xterms-in-ubuntu#comments</comments>
		<pubDate>Sun, 05 Oct 2008 23:56:23 +0000</pubDate>
		<dc:creator>Peter Harkins</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[256 colors]]></category>
		<category><![CDATA[screen]]></category>
		<category><![CDATA[terminals]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[vim]]></category>
		<category><![CDATA[xterm]]></category>

		<guid isPermaLink="false">http://push.cx/?p=327</guid>
		<description><![CDATA[It&#8217;s not commonly used, but most Linux terminals can support 256 colors. It&#8217;s also a bit of a pain in the ass to set up if it doesn&#8217;t Just Work out of the box. Having spent a while today tinkering and searching and cursing and testing and trading mail/IM with folks who understand the eldritch [...]]]></description>
			<content:encoded><![CDATA[<p>
It&#8217;s not commonly used, but most Linux terminals can support 256 colors. It&#8217;s also a bit of a pain in the ass to set up if it doesn&#8217;t Just Work out of the box. Having spent a while today tinkering and searching and cursing and testing and trading mail/IM with folks who understand the eldritch depths of terminals better than I, I thought I&#8217;d write up my findings for anyone else who&#8217;d like spiffy colors.
</p>

<p>
Ncurses and other terminal programming libraries need to know what capabilities your terminal supports, like highlighting, underlining, blinking, and colors. This used to be done with <kbd>termcap</kbd> but now everyone uses <kbd>terminfo</kbd>, which hold the specifications in a set of files. The first thing to do is make sure you have a 256-color terminfo file for your terminal:
</p>

<pre>&nbsp;
$ find /lib/terminfo /usr/share/terminfo -name <span style="color: #ff0000;">"*256*"</span></pre>

<p>
What you need is to see a filed named <kbd><i>terminal</i>-256color</kbd>. For example, mine is <kbd>xterm-256color</kbd>. My first hassle was that Ubuntu doesn&#8217;t install this by default. I can&#8217;t guess what it will be named in your distribution (look for packages with &#8216;ncurses&#8217; or &#8216;term&#8217; in their names), but in Ubuntu it&#8217;s just a quick:
</p>

<pre>&nbsp;
$ sudo aptitude install ncurses-term</pre>

<p>
Xterm needs a little more configuration, edit <kbd>~/.Xdefaults</kbd> to add:
</p>

<pre>
*customization: -color
XTerm*termName:  xterm-256color
</pre>

<p>
To make this apply to new terminals (so you don&#8217;t have to log out and back in), run:
</p>

<pre>&nbsp;
$ xrdb -merge ~/.Xdefaults
<span style="color: #808080; font-style: italic;"># you'll also want to add this to your ~/.xsession so it happens every time you log in:</span>
<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#91;</span> -f <span style="color: #0000ff;">$HOME</span>/.Xdefaults <span style="color: #66cc66;">&#93;</span>; <span style="color: #b1b100;">then</span>
  xrdb -merge <span style="color: #0000ff;">$HOME</span>/.Xdefaults
<span style="color: #b1b100;">fi</span></pre>

<p>
One tricky thing about xterm is that is supports 256 colors via ANSI escape sequences, but they won&#8217;t be available to programs without the right terminfo file. So <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5mcmV4eC5kZS94dGVybS0yNTYtbm90ZXMv">popular test scripts</a> will work while ncurses-based programs will fail. I spent a lot of time catching on to this one. This config file will tell xterm to enable 256 colors and set the TERM environment variable to &#8216;xterm-256color&#8217; so the right terminfo file is used. You can test this with:
</p>

<pre>&nbsp;
$ tput colors
<span style="color: #cc66cc;">256</span>
$ <span style="color: #000066;">echo</span> <span style="color: #0000ff;">$TERM</span>
xterm-256color</pre>

<img src="http://push.cx/wp-content/uploads/2008/10/xterm-colortest.png" alt="Xterm color test" title="xterm-colortest" width="544" height="209" class="size-full wp-image-330 important" />

<p>
At this point, you&#8217;re all set. Individual applications may need some configuring to use 256-color schemes. In vim, <kbd>:set t_Co</kbd> should now report that you have 256 colors available. Run <kbd>locate vim | grep 256</kbd> to find the color schemes available, and <kbd>:colo desert256</kbd> to use one. If none of those suit you, there are <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3d3dy5jcy5jbXUuZWR1L35tYXZlcmljay9WaW1Db2xvclNjaGVtZVRlc3Q=">plenty more</a> available. I put a little code in my <kbd>~/.vimrc</kbd> to only load it when I have colors available, because sometimes I use different terminals.
</p>

<pre>
colorscheme desert
if &amp;t_Co &gt; 2 || has(&quot;gui_running&quot;)
  syntax on
endif
if &amp;t_Co == 256
  colorscheme inkpot
endif
</pre>

<img src="http://push.cx/wp-content/uploads/2008/10/vim-256.png" alt="Vim in 256 colors with colorscheme &#039;inkpot&#039;" title="vim-256" width="478" height="378" class="size-medium wp-image-331 important" />

<p>
GNU screen needs a hint to use 256 colors. You can add the following to your <kbd>~/.screenrc</kbd>, or just wait for Ubuntu Intrepid Ibex later this month to <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cHM6Ly9idWdzLmxhdW5jaHBhZC5uZXQvdWJ1bnR1Lytzb3VyY2Uvc2NyZWVuLytidWcvODc5NjY=">include it by default</a>:
</p>

<pre>&nbsp;
<span style="color: #808080; font-style: italic;"># terminfo and termcap for nice 256 color terminal</span>
<span style="color: #808080; font-style: italic;"># allow bold colors - necessary for some reason</span>
attrcolor b <span style="color: #ff0000;">".I"</span>
<span style="color: #808080; font-style: italic;"># tell screen how to set colors. AB = background, AF=foreground</span>
termcapinfo xterm <span style="color: #ff0000;">'Co#256:AB=<span style="color: #000099; font-weight: bold;">\E</span>[48;5;%dm:AF=<span style="color: #000099; font-weight: bold;">\E</span>[38;5;%dm'</span>
<span style="color: #808080; font-style: italic;"># erase background with current bg color</span>
defbce <span style="color: #ff0000;">"on"</span> 
<span style="color: #808080; font-style: italic;"># set TERM</span>
term screen-256color-bce</pre>

<p>
This was all (of course) an hours-long session of <a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3Byb2plY3RzLmNzYWlsLm1pdC5lZHUvZ3NiL29sZC1hcmNoaXZlL2dzYi1hcmNoaXZlL2dzYjIwMDAtMDItMTEuaHRtbA==">yak shaving</a> because I&#8217;m playing around with a secret project I&#8217;m not yet ready to announce<a href="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?url=aHR0cDovL3JvZ3VlYmFzaW4ucm9ndWVsaWtlZGV2ZWxvcG1lbnQub3JnL2luZGV4LnBocD90aXRsZT1NYWluX1BhZ2U=" style=\"text-decoration: none; color: #0000ff\">.</a>
</p> <img src="http://push.cx/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=327" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://push.cx/2008/256-color-xterms-in-ubuntu/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

