Terminal Escape Code Zen - Strace and Tput

termtrace(){( strace -s 1000 -e write tput $@ 2>&2 2>&1 ) | grep -o '"[^"]*"';}
Depending on the TERM, the terminfo version, ncurses version, etc.. you may be using a varied assortment of terminal escape codes. With this command you can easily find out exactly what is going on.. This is terminal escape zen! ( 2>&2 strace -f -F -e write -s 1000 sh -c 'echo -e "initc\nis2\ncnorm\nrmso\nsgr0" | tput -S' 2>&1 ) | grep -o '"\\[^"]*"' --color=always "\33]4;%p1%d;rgb:%p2%{255}%*%{1000}%/%2.2X/%p3%{255}%*%{1000}%/%2.2X/%p4%{255}%*%{1000}%/%2.2X\33\\\33[!p\33[?3;4l\33[4l\33>\33[?12l\33[?25h\33[27m\33(B\33[m" Lets say you want to find out what you need to echo in order to get the text to blink.. echo -e "`tput blink`This will blink`tput sgr0` This wont" Now you can use this function instead of calling tput (tput is much smarter for portable code because it works differently depending on the current TERM, and tput -T anyterm works too.) to turn that echo into a much faster executing code. tput queries files, opens files, etc.. but echo is very strait and narrow. So now you can do this: echo -e "\33[5mThis will blink\33(B\33[m This wont" More at http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html
Sample Output
$ for cap in `infocmp -1q|sed 's/ *//g'|grep -v '#\||' |cut -d= -f1|tr "\n" ' '`; do echo "$cap: `termtrace $cap 5`"; done
bel: "\7"
blink: "\33[5m"
bold: "\33[1m"
cbt: "\33[Z"
civis: "\33[?25l"
clear: "\33[H\33[2J"
cnorm: "\33[?12l\33[?25h"
cr: "\r"
csr: "\33[6;1r"
cub: "\33[5D"
cub1: "\10"
cud: "\33[5B"
cud1: "\n"
cuf: "\33[5C"
cuf1: "\33[C"
cup: "\33[6;1H"
cuu: "\33[5A"
cuu1: "\33[A"
cvvis: "\33[?12;25h"
dch: "\33[5P"
dch1: "\33[P"
dl: "\33[5M"
dl1: "\33[M"
ech: "\33[5X"
ed: "\33[J"
el: "\33[K"
el1: "\33[1K"
flash: "\33[?5h"
"\33[?5l"
home: "\33[H"
hpa: "\33[6G"
ht: "\t"
hts: "\33H"
ich: "\33[5@"
il: "\33[5L"
il1: "\33[L"
ind: "\n"
indn: "\33[5S"
initc: "\33]4;5;rgb:00/00/00\33\\"
invis: "\33[8m"
is2: "\33[!p\33[?3;4l\33[4l\33>"
kDC: "\33[3;2~"
kEND: "\33[1;2F"
kHOM: "\33[1;2H"
kIC: "\33[2;2~"
kLFT: "\33[1;2D"
kNXT: "\33[6;2~"
kPRV: "\33[5;2~"
kRIT: "\33[1;2C"
kb2: "\33OE"
kbs: "\177"
kcbt: "\33[Z"
kcub1: "\33OD"
kcud1: "\33OB"
kcuf1: "\33OC"
kcuu1: "\33OA"
kdch1: "\33[3~"
kend: "\33OF"
kent: "\33OM"
kf1: "\33OP"
kf10: "\33[21~"
kf11: "\33[23~"
kf12: "\33[24~"
kf13: "\33O2P"
kf14: "\33O2Q"
kf15: "\33O2R"
kf16: "\33O2S"
kf17: "\33[15;2~"
kf18: "\33[17;2~"
kf19: "\33[18;2~"
kf2: "\33OQ"
kf20: "\33[19;2~"
kf21: "\33[20;2~"
kf22: "\33[21;2~"
kf23: "\33[23;2~"
kf24: "\33[24;2~"
kf25: "\33O5P"
kf26: "\33O5Q"
kf27: "\33O5R"
kf28: "\33O5S"
kf29: "\33[15;5~"
kf3: "\33OR"
kf30: "\33[17;5~"
kf31: "\33[18;5~"
kf32: "\33[19;5~"
kf33: "\33[20;5~"
kf34: "\33[21;5~"
kf35: "\33[23;5~"
kf36: "\33[24;5~"
kf37: "\33O6P"
kf38: "\33O6Q"
kf39: "\33O6R"
kf4: "\33OS"
kf40: "\33O6S"
kf41: "\33[15;6~"
kf42: "\33[17;6~"
kf43: "\33[18;6~"
kf44: "\33[19;6~"
kf45: "\33[20;6~"
kf46: "\33[21;6~"
kf47: "\33[23;6~"
kf48: "\33[24;6~"
kf49: "\33O3P"
kf5: "\33[15~"
kf50: "\33O3Q"
kf51: "\33O3R"
kf52: "\33O3S"
kf53: "\33[15;3~"
kf54: "\33[17;3~"
kf55: "\33[18;3~"
kf56: "\33[19;3~"
kf57: "\33[20;3~"
kf58: "\33[21;3~"
kf59: "\33[23;3~"
kf6: "\33[17~"
kf60: "\33[24;3~"
kf61: "\33O4P"
kf62: "\33O4Q"
kf63: "\33O4R"
kf7: "\33[18~"
kf8: "\33[19~"
kf9: "\33[20~"
khome: "\33OH"
kich1: "\33[2~"
kind: "\33[1;2B"
kmous: "\33[M"
knp: "\33[6~"
kpp: "\33[5~"
kri: "\33[1;2A"
mc0: "\33[i"
mc4: "\33[4i"
mc5: "\33[5i"
meml: "\33l"
memu: "\33m"
op: "\33[39;49m"
rc: "\0338"
rev: "\33[7m"
ri: "\33M"
rin: "\33[5T"
rmacs: "\33(B"
rmam: "\33[?7l"
rmcup: "\33[?1049l"
rmir: "\33[4l"
rmkx: "\33[?1l\33>"
rmso: "\33[27m"
rmul: "\33[24m"
rs1: "\33c"
rs2: "\33[!p\33[?3;4l\33[4l\33>"
sc: "\0337"
setab: "\33[45m"
setaf: "\33[35m"
sgr0: "\33(B\33[m"
smacs: "\33(0"
smam: "\33[?7h"
smcup: "\33[?1049h"
smir: "\33[4h"
smkx: "\33[?1h\33="
smso: "\33[7m"
smul: "\33[4m"
tbc: "\33[3g"
u6: "\33[5;0R"
u7: "\33[6n"
u8: "\33[?1;2c"
u9: "\33[c"
vpa: "\33[6d"

These Might Interest You

  • This is super fast and an easy way to test your terminal for 256 color support. Unlike alot of info about changing colors in the terminal, this uses the ncurses termcap/terminfo database to determine the escape codes used to generate the colors for a specific TERM. That means you can switch your terminal and then run this to check the real output. tset xterm-256color at any rate that is some super lean code! Here it is in function form to stick in your .bash_profile aa_256 () { ( x=`tput op` y=`printf %$((${COLUMNS}-6))s`; for i in {0..256}; do o=00$i; echo -e ${o:${#o}-3:3} `tput setaf $i;tput setab $i`${y// /=}$x; done ) } From my bash_profile: http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html Show Sample Output


    7
    ( x=`tput op` y=`printf %$((${COLUMNS}-6))s`;for i in {0..256};do o=00$i;echo -e ${o:${#o}-3:3} `tput setaf $i;tput setab $i`${y// /=}$x;done; )
    AskApache · 2010-09-06 10:39:27 2
  • Very useful for interactive scripts where you would like to return the terminal contents to its original state before the script was run. This would be similar to how vi exits and returns you to your original terminal screen. Save and clear the terminal contents with: tput smcup Execute some commands, then restore the saved terminal contents with: tput rmcup


    8
    tput smcup; echo "Doing some things..."; sleep 2; tput rmcup
    jgc · 2009-10-08 16:48:04 2
  • Print a row of characters across the terminal. Uses tput to establish the current terminal width, and generates a line of characters just long enough to cross it. In the example '#' is used. It's possible to use a repeating sequence by dividing the columns by the number of characters in the sequence like this: seq -s'~-' 0 $(( $(tput cols) /2 )) | tr -d '[:digit:]' or seq -s'-~?' 0 $(( $(tput cols) /3 )) | tr -d '[:digit:]' You will lose chararacters at the end if the length isn't cleanly divisible. Show Sample Output


    6
    seq -s'#' 0 $(tput cols) | tr -d '[:digit:]'
    jgc · 2010-04-01 09:06:44 0
  • This will use tput to place the command (date %T in this case) in the upper right corner of the terminal


    0
    while sleep 1;do tput sc;tput cup 0 $(($(tput cols)-11));echo -e "\e[31m`date +%T`\e[39m";tput rc;done &
    x3mboy · 2017-11-16 18:07:39 0

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: