10

I'm fairly new at programming, but I've wondered how shell text editors such as vim, emacs, nano, etc are able to control the command-line window. I'm primarily a Windows programmer, so maybe it's different on *nix. As far as I know, it's only possible to print text to a console, and ask for input. How do text editors create a navigable, editable window in a command line environment?

1
  • Making a text editor in Windows Shell: just download the now free visual basic dos 1, you can easily make a edit.com clone with that. -OR- For those who want to make editors in *nix/bsd/osx scenarios: ncurses has a fine method for doing this, using 'pads' to hold the entire document (or to hold the largest amount it can in memory before buffering), this allows you to essentially copy a 'window' of the buffer to screen and even scroll it around, enter text, etc. A good book to read is: Programmer's Guide to NCurses ISBN 978-0470107591 (ncurses is a LOT easier than you may think!) May 5, 2015 at 11:02

8 Answers 8

10

By using libraries such as the following which, in turn, use escape character sequences

NAME
       ncurses - CRT screen handling and optimization package

SYNOPSIS
       #include 

DESCRIPTION
       The  ncurses library routines give the user a terminal-independent 
method of updating character screens with reasonable optimization.  This 
implementation is ‘‘new curses’’ (ncurses) and is the approved replacement 
for 4.4BSD classic curses, which has been discontinued.

[...snip....]

       The ncurses package supports: overall screen, window and pad 
manipulation; output to windows and pads; reading terminal input; control 
over terminal and curses input and output  options;  environment query 
routines; color manipulation; use of soft label keys; terminfo capabilities; 
and access to low-level terminal-manipulation routines.

6

Short answer: there are libraries for it (like curses, slang).

Longer answer: doing things like jumping around with the cursor or changing colors are done by printing special character sequences (called escape-secquences, because they start with the ESC character).

4

Learning about ncurses might be a good starting point.

4

There is an old protocol called vt100 based on a "VT100" terminal. It used codes starting with escape to control cursor position, color, clearing the screen, etc.

It's also how you get colored prompts.

Google VT100 or "terminal escape codes"

edit: I Googled it for you: http://www.termsys.demon.co.uk/vtansi.htm

3

You will also notice this if you type "edit" in a Windows command line console. This "feature" is not unique to unix-like systems, though the concepts for manipulating the windows console in that way are quite different to in unix.

2

On Unix systems, a console window emulates an ancient serial terminal (usually a VT100). You can print special control characters and escape sequences to move the cursor around, change colors, and do other special effects. There are libraries to help handle the details; ncurses is the most popular.

On Windows, the [Win32 Console API](http://msdn.microsoft.com/en-us/library/ms682073(VS.85%29.aspx) provides similar functionality, but in a rather different manner.

2

Type "c:\winnt\system32\edit" or "c:\windows\system32\edit" at the command line, and you'll be shown a command line text editor.

People are mostly right about the ESC character being used to control the command screen, but some older programs also write characters directly to the memory space used by the Windows Command Line screen.

In order to control the command line window, you used to have to write your own windowing forms, entry box, menus, etc. You'd also have to wrap all that up in a big loop for handling events.

0

More Windows command line specific, the app typically calls DOS or BIOS functions that do the same. Sometimes ANSI command code support is available, sometimes it isn't (depending on exact MS OS version and whether or not it's configured to load it).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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