OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Moving The Cursor

Escape codes can be used to position the cursor. This is commonly used to provide a clock or some other kind of information at a different location on the screen, such as an upper corner each time the prompt is drawn. Here is a list of the escape codes that posi- tion the cursor:


Table 13-4: Cursor Movement Escape Sequences

Escape Code Action

\033[l;cH Move the cursor to line l and column c


image

\033[nA Move the cursor up n lines


image

\033[nB Move the cursor down n lines


image

\033[nC Move the cursor forward n characters


image

\033[nD Move the cursor backward n characters


image

\033[2J Clear the screen and move the cursor to the upper left corner (line 0, column 0)


image

\033[K Clear from the cursor position to the end of the current line


image

\033[s Store the current cursor position


image

\033[u Recall the stored cursor position


image


Using the codes above, we'll construct a prompt that draws a red bar at the top of the screen containing a clock (rendered in yellow text) each time the prompt is displayed. The code for the prompt is this formidable looking string:



PS1="\[\033[s\033[0;0H\033[0;41m\033[K\033[1;33m\t\033[0m\033[u\]

<\u@\h \W>\$ "

PS1="\[\033[s\033[0;0H\033[0;41m\033[K\033[1;33m\t\033[0m\033[u\]

<\u@\h \W>\$ "


Let's take a look at each part of the string to see what it does:

Moving The Cursor


Table 13-5: Breakdown Of Complex Prompt String


Sequence Action

Sequence Action

\[ Begins a non-printing character sequence. The purpose of this is to allow bash to properly calculate the size of the visible prompt. Without an accurate calculation, command line editing features cannot position the cursor correctly.


image

\033[s Store the cursor position. This is needed to return to the prompt location after the bar and clock have been drawn at the top of the screen. Be aware that some terminal emulators do not recognize this code.


image

\033[0;0H Move the cursor to the upper left corner, which is line 0, column 0.


image

\033[0;41m Set the background color to red.


image

\033[K Clear from the current cursor location (the top left corner) to the end of the line. Since the background color is now red, the line is cleared to that color creating our bar. Note that clearing to the end of the line does not change the cursor position, which remains at the upper left corner.


image

\033[1;33m Set the text color to yellow.


image

\t Display the current time. While this is a “printing” element, we still include it in the non-printing portion of the prompt, since we don't want bash to include the clock when calculating the true size of the displayed prompt.


image

\033[0m Turn off color. This affects both the text and background.


image

\033[u Restore the cursor position saved earlier.


image

\] End the non-printing characters sequence.


image

<\u@\h \W>\$ Prompt string.


image


Top OS Cloud Computing at OnWorks: