[clug] Terminal Control Sequences

Alex Satrapa grail at goldweb.com.au
Mon Jul 5 00:55:53 MDT 2010


On 05/07/2010, at 16:06 , Kevin Pulo wrote:

> For the function above, you'd want
> 
>    function settitle {
>        PS1="\[\e]2;penguin\a\]\u@\h:\$ "
>        echo "Done."
>    }
> 
> Though why it warrants a function and shouldn't just be entered
> directly is beyond me.

The equivalent in ZSH is to insert %{…%} around the zero-width columns.

I put this kind of stuff in a function because it's very easy to make mistakes when editing a command line prompt that looks like this:


PS1=`echo "%{\\e[$1m[%n@%m:%~]\\e[0m\n\\e[$2m%}%D{%k:%M} [%?|%!]%#%{\\e[0m%} "`

I can read it just fine, but then again I enjoy programming in Perl so I have a fetish for line-noise. Note that I found it easier to include that as an eval rather than attempting to properly escape all the ZSH special characters (!) and then properly escape all escapes for the stuff that was supposed to be escaped.

So rather than type that out when you want to use it, define it in a function. Get it right once, remove the opportunities for typos, transcription errors, spelling mistakes, or misread symbols. Hey, is '~' a tilde, hyphen, minus-sign, em-dash, swung dash or a bad pixel? Is that "|" a pipe, lower-case L, upper-case i, numeral 1, Spanish exclamation ¡, or something else entirely?

There are a surprisingly large number of fonts in which a tilde is drawn at the same height as the hyphen (~ and -), so even without the ambiguity induced by small point sizes, it can be hard to disambiguate between tilde (~) and swung-dash (⁓) (and in some pathological cases, a hyphen (-)).

Wrap dangerous/tricky code up in a function, and give it a meaningful name. I for one find it easier to type "two-line-prompt 33 44" than PS1=`echo "%{\\e[33m[%n@%m:%~]\\e[0m\n\\e[44m%}%D{%k:%M} [%?|%!]%#%{\\e[0m%} "`

And for people wanting to experiment with ANSI terminal colour codes, this script will produce every combination of background/foreground colour, including bold foregrounds:
  http://www.pixelchaos.net/2007/06/22/show_colors/

And for those people considering designing their own fonts: please, for the love of all that is holy, make sure it is easy to disambiguate these characters: (B,8), (b,6), (-,~), (~,⁓), (l,i,I,!,|,L,1), (O,0), (7,T), (f,t), (5,S), (2,Z), (T,Y), (Q,O), (q,9), ((,[,{), (,,.), (:,;), (',’,‘) … ah hang it.

Alex



More information about the linux mailing list