How do shell text editors work?

2019-04-07 09:06发布

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?

8条回答
相关推荐>>
2楼-- · 2019-04-07 09:21

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.

查看更多
甜甜的少女心
3楼-- · 2019-04-07 09:23

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.

查看更多
不美不萌又怎样
4楼-- · 2019-04-07 09:27

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

查看更多
戒情不戒烟
5楼-- · 2019-04-07 09:31

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).

查看更多
\"骚年 ilove
6楼-- · 2019-04-07 09:33

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.

查看更多
Juvenile、少年°
7楼-- · 2019-04-07 09:37

Learning about ncurses might be a good starting point.

查看更多
登录 后发表回答