How do command-line applications like Vim work? Specifically, how do they take control of the terminal in the manner they do? Also, for future reference, is there a specific term for applications that take control of the terminal the way Vim does?

  • 1
    A term I've seen used for such programs is full-screen terminal applications. The word terminal tries to indicate it's a text display rather than GUI, and full-screen indicates the program moves the terminal's cursor around the screen rather than merely writing one line at a time (and depending on the terminal to scroll the accumulated lines).
    – Sotto Voce
    Nov 2, 2022 at 9:04
  • 8
    In my mind, you're asking about an interactive opposite-of-a-CLI! A hallmark of a CLI is that you type a command line, and when you're happy with it, you hit RETURN. But I think what you're asking about is programs that process each key immediately as you hit it. They do that (under Unix/Linux, anyway) by putting the terminal driver into "raw" or "cbreak" mode. Nov 2, 2022 at 15:26
  • 9
    I've seen "TUI" used for such text-based user interfaces (to distinguish it from a pixel-based GUI).
    – R.M.
    Nov 2, 2022 at 21:27

2 Answers 2


vim and other semi-graphical (the capability to display semi-graphic characters such as corners, full crosses…) applications control the terminal (manage the position of the cursor, the position of displayable characters, color settings…) by sending dedicated escape sequences, control codes that the terminal will translate into some dedicated action it will execute.

Because it would be just a nightmare for any programmer to echo the escape sequences to the stdout, not to say implying non portable code since there have always been many different terminals with different capabilities and different escape sequences,

a library abstracting all that work was created : curses. Nowadays named ncurses which also provides higher level functions such as window management.

ncurses relies on the terminfo database for acquiring the appropriate terminal description of capabilities.

It is thanks to this library of functions that vim, iptraf-ng, alsa amixer, less, gdb, the primary kernel configuration utility and many other control the terminal.

Note that these apps are typically not called "command-line" utilities which generally handle a single line of input with basic cursor management and editing facilities thanks to the readline library.
Per contrast and as you can read in the ncurses man page linked hereabove, these programs can be called : Interactive, Screen Oriented. vim is typically named a screen-oriented editor per contrast to ed the line-oriented editor.

Note following suggestion in comments :

When started, the application will inherit the tty driver settings from the shell that launched it which are likely to be very similar to those initially set by the original agetty.
This including buffering of input until catching a newline charater, echoing of input keys at instant cursor's position… all sort of features whatever screen oriented application is not likely to want

When initializing, the program will save current tty driver settings and force those according to the programmer wishes.
Before quitting… the programmer is strongly invited to restore the initial settings… unless facing the risk of coming back to the calling shell in rather unpredictable but certainly messy conditions…

  • Another piece of the puzzle is how the application turns off the tty driver's pre-processing (of keystroke input from the terminal) and post-processing (of display output to the terminal). And, of course, remembers the original tty driver settings in order to restore them when the application quits.
    – Sotto Voce
    Nov 2, 2022 at 9:10
  • @SottoVoce : Do you mean what def/reset_prog_mode() achieve behind the scene ?
    – MC68020
    Nov 2, 2022 at 9:30
  • 2
    Or at the shell level, stty raw, stty cooked, and similar operations Nov 2, 2022 at 9:59
  • @MC68020 I'm not familiar with the routine you mentioned. I was thinking of the calls to do the equivalents of the stty commands as @roaima said, though I think for a C/C++ program like vim they're ioctl() calls to get and set the tty line disiplines.
    – Sotto Voce
    Nov 2, 2022 at 16:08
  • If I am not mistaken, I think the name of the interactive terminal applications that the OP and others here are looking for is in the tags for the question: console application - Am I right?
    – B.Kaatz
    Nov 3, 2022 at 1:52

One character at a time goes into a buffer.
When the carriage return is entered, the buffer is examined ("parsed") one character at a time looking for tokens arranged in a given syntax.
Then, if possible, some command is executed accordingly. If the input can't be parsed, then an error message is given showing what the command interpreter couldn't figure out how to handle.

Rinse and repeat, until the command line interpreter is halted and/or the computer is shut down.

Basically, it's like any other computer program. Inputs, IF and ELSE statements, data manipulation (doing calculations and moving data around in memory), and outputs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .