Skip to content
Advertisement

How does tmux, vim, emacs, etc transcend the UI limitations of *nix terminals?

When I’m writing a program for use on the command line, I notice that there’s some limitations. For instance, I can’t draw a 1-pixel-thick horizontal or vertical line like tmux does when it separates panes in a window. I can only move the cursor down, not up like VI seemingly does. I can’t refresh information on the top of the page if the cursor is at the bottom.

So, when programs like tmux and vi do this, I have to wonder if they are:

  • drawing the screen from top to bottom every update (which I think is highly unlikely because otherwise I could scroll up in my terminal and see each redraw)

  • using some library that enables graphics in the terminal, like SDL, which I also think is unlikely.

  • using some standard syscall I don’t know about

or finally

  • taking advantage of some feature of Linux/Unix of which I’m completely unaware.

So, how do these programs generate such a rich UI in such a seemingly limited shell? So long as the answer gives me just enough fodder to go on a Google rampage, I’ll be happy.

I’m also assuming that these programs use some common method to do these things, but if that’s wrong let me know.

Advertisement

Answer

A typical terminal emulator has a lot more features than are immediately apparent.

Essentially a program just needs to output short sequences of bytes that represent various commands such as move cursor (up|down|left|right), change color, scroll region, erase region, etc.

These commands typically begin with the escape character (the same character that is generated when you press the esc key while typing in a terminal) followed by various other characters, depending on which action is desired.

A good starting point for understanding how it works would be the Wikipedia Article about ANSI escape codes

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement