shell programming - screen and cursor control

created onJanuary 19, 2022

Screen and cursor control is done with

capability ANSI Escape Sequence Description
reset reset all text attributes, clear screen and move cursor to home

cursor movement and control

capability ANSI Escape Sequence Description
sc \E7 save cursor position
rc \E8 restore saved cursor position
clear \E[H\E[2J clear screen and move cursor to top left
cuu #1 \E[#1A move cursor up #1 rows
cud #1 \E[#1B move cursor down #1 rows
cuf #1 \E[#1C move cursor right #1 columns
cub #1 \E[#1D move cursor left #1 columns
home \E[H move cursor to top left
hpa #1 \E[#1G move cursor to column #1
vpa #1 \E[#1d move cursor to row #1, first column
cup #1 #2 \E[#1;#2H move cursor to row #1, column #2
cup #1 #2 \E[#1;#2H move cursor to row #1, column #2
civis set cursor invisible
cnorm set cursor normal

screen control

capability ANSI Escape Sequence Description
clear clear the whole screen
ech #1 \E#1X erase #1 characters without moving cursor
dch #1 \E#1P delete #1 characters
ed \E[J clear from cursor to end of screen
el \E[K clear from cursor to end of line
el1 \E[1K clear from cursor to start of line
smcup save and clear screen
rmcup restore saved screen

text attributes

capability ANSI Escape Sequence Description
blink \E[5m blinking text on
bold \E[1m bold text on
dim \E[2m dim text on
rev \E[7m reverse video on (switch text/background colors)
sitm \E[3m italic text on
ritm \E[23m italic text off
smso \E[7m highlighted text on
rmso \E[27m highlighted text off
smul \E[4m underlined text on
rmul \E[24m underlined text off
invis invisible text
setb #1 set background color #1
setf #1 set text color #1
setab #1 \E[4#1m set background color #1 using ANSI escape
setaf #1 \E[3#1m set text color #1 using ANSI escape
sgr0 \E(B\E[m reset text attributes

colors for , , and in 8 color terminals

color code color
0 Black
1 Red
2 Green
3 Yellow
4 Blue
5 Magenta
6 Cyan
7 White
8 Not used
9 Reset to default color