4

I can't figure out how is implemented programs such as Vim (or top for example) that are executed inside the terminal and has a GUI. It is assumed that the terminal can only display characters, and Vim can not only show multiple windows, also can handle the cursor moving it in all directions.

Another example is the linux top utility that shows information in real time that is updated, how is possible that this program can update the information instead of making a scroll down and showing new printed characters?.

  • Keyword: ANSI codes – phk Oct 30 '16 at 10:51
4

vim and gvim may be separate executables, linked with different libraries. It is possible to have one executable doing either interface (elvis and emacs do this for example). vim 4.0 in 1996 added a -g option for telling it to use the GUI version (which in this case would be part of the same executable).

I did not find a copy of the announcement for 4.0 (which might have given some clues regarding the motivation for the -g option (vim's announcements mailing list started in 1997), but see it mentioned in an old FAQ by Laurent Duperval:

7.3 How can I make Vim faster on a Unix station?

   The GUI support in Vim 4.0 can slow down the startup time noticeably.
   Until Vim supports dynamic loading, you can speed up the startup time
   by compiling two different versions of Vim: one with the GUI and one
   without the GUI and install both. Make sure you remove the link from
   $bindir/gvim to $bindir/vim when installing the GUI version, though.

   If screen updating is your problem, you can run Vim in screen. screen
   is an ascii terminal multiplexer. The latest version can be found at
   <URL:ftp://ftp.uni-erlangen.de:/pub/utilities/screen>.

My recollection is that for quite a while, there were two executables (when that changed would require quite a lot of research into the actual packages used). But the capability was there starting in 1996.

Given either type of interface, there are ways to update the display. For gvim, that uses the X libraries, while terminal applications such as top (or vim) use escape sequences. Depending on the system, both of these are termcap applications, obtaining their repertoire of escape sequences using the termcap interface of ncurses, etc. (some versions of top actually use ncurses for display, e.g., htop). vim augments that repertoire using builtin-tables (which often are redundant).

Interestingly, the procps version of top in Debian is (a relative rarity) a terminfo application as can be seen by inspecting its source-code.

  • 1
    You can run gvim in a terminal without X. It will complain it can't open a display, but then it will fallback to the terminal interface. For what it's worth. – Satō Katsura Oct 30 '16 at 10:14
  • hmm - didn't use to be - I'll point out when it changed. – Thomas Dickey Oct 30 '16 at 10:16
  • 1
    ncurses is the answer! until just now I didn't know that there were specific libraries to create GUIs inside the terminal. I'm creating now a very simple window example from here: link Thanks! I don't have enough reputation to upvote. – PlainOldProgrammer Oct 30 '16 at 10:27
  • @PlainOldProgrammer please do select your preferred answer by clicking on the tickmark :). I considered mentioning termcap... I really like the link here to how closely these libraries are related. – sourcejedi Oct 30 '16 at 15:20
  • Now that you mention it, I can recall switching from Vim to Elvis about 1996, because Elvis was noticeably faster than Vim. Though I believe the biggest bottleneck at the time was syntax highlighting, not startup (and to some extent it still is, some highlighting patterns in Vim remained quite awful to this day). It's sad Elvis didn't get anywhere. – Satō Katsura Oct 30 '16 at 15:53
2

Applications running in a terminal can have a text user interface because most terminals are not just dumb terminals that can only print characters and move to the next line, but “intelligent” terminals that support control characters and escape sequences with meanings such as “erase the current line and scroll subsequent lines upwards”, “move to position (53,7)””, “from now on print in bright red”, etc.

Most terminal emulators implement most of the escape sequences of xterm, itself mostly compatible with the VT100 hardware terminal and its successors. Applications know what escape sequences to send to perform various actions via the termcap or terminfo databases. See How do keyboard input and text output work? (section “Text mode application, running in a terminal”) and How and where is $TERM interpreted? for more details.

In addition to having a text user interface, Vim also has a graphical user interface called GVim. This interface runs on a graphical terminal that implements the X11 protocol, not on a text terminal (but of course you can launch the application from within a text terminal, it just won't display in that terminal).

1

Subsequent to the original TTY, terminals have become available which replace the printer with a VDU.

A VDU has slightly different characteristics to paper. Characters on the display can be erased and replaced, not just over-striked.

ASCII provided sufficient control characters for a basic line printer, but did not anticipate the VDU. Additional terminal-specific controls are encoded in ASCII using an "escape". An escape sequence is started with an ASCII escape character. The following characters in the sequence are interpreted as terminal-specific controls.

The visual editor vi is a good example of an application which uses this new technology.

Sets of escape codes are being standardized. If you have recent enough documentation, "ANSI escape" might be a good term to look up in the index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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