brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: tput civis hides the cursor while a script draws, and tput cnorm brings it back

Terminal
~/tips/tip-tput-civis-hide-cursor

tput civis

Hides the terminal cursor so a progress line does not flicker, until you restore it.

A script that rewrites one line of output, a counter or a spinner, looks a bit nervous with the cursor jumping along. tput can hide it, and the commands are readable names instead of escape codes.

tput civis
# ... draw your progress line here ...
tput cnorm

The names come from the terminfo database, which tput uses to find the right codes for your terminal. The terminfo manual lists civis as “make cursor invisible” and cnorm as “make cursor appear normal (undo civis/cvvis)”. There is also el, “clear to end of line”, which helps when a new line of text is shorter than the one before it.

The terminal type comes from $TERM, and -T overrides it. With TERM=xterm the commands print these sequences, shown here through cat -v:

tput civis   ^[[?25l
tput cnorm   ^[[?12l^[[?25h
tput el      ^[[K

A small pattern for a script:

trap 'tput cnorm' EXIT
tput civis
for i in 1 2 3 4 5; do
  printf '\rstep %s of 5' "$i"
  sleep 1
done
printf '\n'

The trap line is the important one. If the script is stopped with Ctrl-C and nothing runs tput cnorm, the cursor stays hidden in your shell. Typing tput cnorm by hand brings it back, but it is better not to need that.

One caveat: when output goes to a file or a pipe instead of a terminal, the escape codes land in that file. Guard the calls with [ -t 1 ] if your script can run from cron or in a pipe.

Further reading: tput(1) manual, terminfo(5) manual

More Terminal tips "copyOnSelect": true "multiLinePasteWarning": true, script -qc 'ls --color=auto -d .' /dev/null | cat -v ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group All Terminal tips (124) All topics

Link