Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to wrap my head how shells move the screen cursor around (Moving around input the arrow keys and such).

I've been doing a lot of testing, and I haven't found any system call that allows for movement of the screen cursor in a terminal. Is this done using specific terminal extensions? If so, is there any documentation as to how this is achieved for, say, xterm?

This has been driving me crazy, any light you can shed on this is appreciated.

share|improve this question

3 Answers 3

up vote 7 down vote accepted

There is no such system call, or set of system calls. It's all done by convention.

Back in the old days, when a "terminal" was a large clunky piece of equiment linked to a computer via a cable, "smart terminals" would do things like move the cursor, or draw a line, or fill in a polygon with colors. The smart terminal usually did this via "escape sequenes". A program issued a special sequence of byte values, usually beginning with ASCII 0x1b, 'ESC', to move a cursor, or color a word, or some special thing other than imitating a teletypewriter.

That's the "convention". Nowadays, a "terminal" is almost always just one window among many on a bitmapped screen, xterm, rxvt, etc etc. It's up to xterm (or rxvt or whatever) to read the bytes coming from "the program" running in it, and interpret those bytes according to a given convention. I just typed this in one of my xterm windows:

6 % echo $TERM 
xterm

But I know that old, old xterms used to say "vt100" in that case. Any program that "moves the cursor" has to output escape sequences for the "xterm" convention to move the cursor in an xterm instance.

You could do man curses and man terminfo for more information.

PS

People have done whole windowing systems with escape sequences. See MGR as an example.

share|improve this answer
2  
You can read the list of xterm escape sequences. (Not a tutorial!) Nowadays most terminal emulators mimic xterm, or at least a large subset thereof. –  Gilles May 10 '11 at 22:07

I know this is not the exact answer to your question but the curses library is the stuff you need to do some tui dev. If you're a programmer, looking at its source (available in all open-source unix distributions) will show you how it's done.

share|improve this answer

Checkout tput. It is a wrapper around ANSI Escape sequences that are supported (the amount of support differs a lot) by almost all terminal emulators.

tput will only output the sequence if it is supported by the terminal, therefore you don't have to worry about the support. Or at least you don't have to worry about strange characters being printed in the terminal :-)

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.