269 lines
5.0 KiB
HTML
269 lines
5.0 KiB
HTML
<HTML
|
|
><HEAD
|
|
><TITLE
|
|
>Cursor Movement</TITLE
|
|
><META
|
|
NAME="GENERATOR"
|
|
CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK
|
|
REL="HOME"
|
|
TITLE="Bash Prompt HOWTO"
|
|
HREF="index.html"><LINK
|
|
REL="UP"
|
|
TITLE="ANSI Escape Sequences: Colours and Cursor Movement"
|
|
HREF="c327.html"><LINK
|
|
REL="PREVIOUS"
|
|
TITLE="Colours"
|
|
HREF="x329.html"><LINK
|
|
REL="NEXT"
|
|
TITLE="Xterm Title Bar Manipulations"
|
|
HREF="xterm-title-bar-manipulations.html"></HEAD
|
|
><BODY
|
|
CLASS="SECT1"
|
|
BGCOLOR="#FFFFFF"
|
|
TEXT="#000000"
|
|
LINK="#0000FF"
|
|
VLINK="#840084"
|
|
ALINK="#0000FF"
|
|
><DIV
|
|
CLASS="NAVHEADER"
|
|
><TABLE
|
|
SUMMARY="Header navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TH
|
|
COLSPAN="3"
|
|
ALIGN="center"
|
|
>Bash Prompt HOWTO: </TH
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="left"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="x329.html"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="80%"
|
|
ALIGN="center"
|
|
VALIGN="bottom"
|
|
>Chapter 6. ANSI Escape Sequences: Colours and Cursor Movement</TD
|
|
><TD
|
|
WIDTH="10%"
|
|
ALIGN="right"
|
|
VALIGN="bottom"
|
|
><A
|
|
HREF="xterm-title-bar-manipulations.html"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"></DIV
|
|
><DIV
|
|
CLASS="SECT1"
|
|
><H1
|
|
CLASS="SECT1"
|
|
><A
|
|
NAME="AEN361"
|
|
></A
|
|
>6.2. Cursor Movement</H1
|
|
><P
|
|
>ANSI escape sequences allow you to move the cursor around the screen at
|
|
will. This is more useful for full screen user interfaces generated by
|
|
shell scripts, but can also be used in prompts. The movement escape
|
|
sequences are as follows:</P
|
|
><TABLE
|
|
BORDER="1"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="SCREEN"
|
|
>- Position the Cursor:
|
|
\033[<L>;<C>H
|
|
Or
|
|
\033[<L>;<C>f
|
|
puts the cursor at line L and column C.
|
|
- Move the cursor up N lines:
|
|
\033[<N>A
|
|
- Move the cursor down N lines:
|
|
\033[<N>B
|
|
- Move the cursor forward N columns:
|
|
\033[<N>C
|
|
- Move the cursor backward N columns:
|
|
\033[<N>D
|
|
|
|
- Clear the screen, move to (0,0):
|
|
\033[2J
|
|
- Erase to end of line:
|
|
\033[K
|
|
|
|
- Save cursor position:
|
|
\033[s
|
|
- Restore cursor position:
|
|
\033[u</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><P
|
|
>The latter two codes are NOT honoured by many terminal emulators. The only
|
|
ones that I'm aware of that do are xterm and nxterm - even though the
|
|
majority of terminal emulators are based on xterm code. As far as I can
|
|
tell, rxvt, kvt, xiterm, and Eterm do not support them. They are supported
|
|
on the console.</P
|
|
><P
|
|
>Try putting in the following line of code at the prompt (it's a little
|
|
clearer what it does if the prompt is several lines down the terminal when
|
|
you put this in):
|
|
<TT
|
|
CLASS="USERINPUT"
|
|
><B
|
|
>echo -en "\033[7A\033[1;35m BASH \033[7B\033[6D"</B
|
|
></TT
|
|
>
|
|
This should move the cursor seven lines up screen, print the word
|
|
"<TT
|
|
CLASS="COMPUTEROUTPUT"
|
|
> BASH </TT
|
|
>", and then return to where it
|
|
started to produce a normal prompt. This isn't a prompt: it's just a
|
|
demonstration of moving the cursor on screen, using colour to emphasize
|
|
what has been done. </P
|
|
><P
|
|
>Save this in a file called "clock":</P
|
|
><TABLE
|
|
BORDER="0"
|
|
BGCOLOR="#E0E0E0"
|
|
WIDTH="100%"
|
|
><TR
|
|
><TD
|
|
><FONT
|
|
COLOR="#000000"
|
|
><PRE
|
|
CLASS="PROGRAMLISTING"
|
|
>#!/bin/bash
|
|
|
|
function prompt_command {
|
|
let prompt_x=$COLUMNS-5
|
|
}
|
|
|
|
PROMPT_COMMAND=prompt_command
|
|
|
|
function clock {
|
|
local BLUE="\[\033[0;34m\]"
|
|
local RED="\[\033[0;31m\]"
|
|
local LIGHT_RED="\[\033[1;31m\]"
|
|
local WHITE="\[\033[1;37m\]"
|
|
local NO_COLOUR="\[\033[0m\]"
|
|
case $TERM in
|
|
xterm*)
|
|
TITLEBAR='\[\033]0;\u@\h:\w\007\]'
|
|
;;
|
|
*)
|
|
TITLEBAR=""
|
|
;;
|
|
esac
|
|
|
|
PS1="${TITLEBAR}\
|
|
\[\033[s\033[1;\$(echo -n \${prompt_x})H\]\
|
|
$BLUE[$LIGHT_RED\$(date +%H%M)$BLUE]\[\033[u\033[1A\]
|
|
$BLUE[$LIGHT_RED\u@\h:\w$BLUE]\
|
|
$WHITE\$$NO_COLOUR "
|
|
PS2='> '
|
|
PS4='+ '
|
|
}</PRE
|
|
></FONT
|
|
></TD
|
|
></TR
|
|
></TABLE
|
|
><P
|
|
>This prompt is fairly plain, except that it keeps a 24 hour clock in the
|
|
upper right corner of the terminal (even if the terminal is resized). This
|
|
will NOT work on the terminal emulators that I mentioned that don't accept
|
|
the save and restore cursor position codes. If you try to run this prompt
|
|
in any of those terminal emulators, the clock will appear correctly, but
|
|
the prompt will be trapped on the second line of the terminal.</P
|
|
><P
|
|
>See also <A
|
|
HREF="the-elegant-useless-clock-prompt.html"
|
|
>Section 12.9</A
|
|
> for a
|
|
more extensive use of these codes.</P
|
|
></DIV
|
|
><DIV
|
|
CLASS="NAVFOOTER"
|
|
><HR
|
|
ALIGN="LEFT"
|
|
WIDTH="100%"><TABLE
|
|
SUMMARY="Footer navigation table"
|
|
WIDTH="100%"
|
|
BORDER="0"
|
|
CELLPADDING="0"
|
|
CELLSPACING="0"
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="x329.html"
|
|
ACCESSKEY="P"
|
|
>Prev</A
|
|
></TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="index.html"
|
|
ACCESSKEY="H"
|
|
>Home</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="xterm-title-bar-manipulations.html"
|
|
ACCESSKEY="N"
|
|
>Next</A
|
|
></TD
|
|
></TR
|
|
><TR
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="left"
|
|
VALIGN="top"
|
|
>Colours</TD
|
|
><TD
|
|
WIDTH="34%"
|
|
ALIGN="center"
|
|
VALIGN="top"
|
|
><A
|
|
HREF="c327.html"
|
|
ACCESSKEY="U"
|
|
>Up</A
|
|
></TD
|
|
><TD
|
|
WIDTH="33%"
|
|
ALIGN="right"
|
|
VALIGN="top"
|
|
>Xterm Title Bar Manipulations</TD
|
|
></TR
|
|
></TABLE
|
|
></DIV
|
|
></BODY
|
|
></HTML
|
|
> |