Center current line in terminal emulator

terminal-emulator

One feature of emacs buffers (even ansi-term, shell, and eshell) that I really like is the ability of C-l to "center" the current line in the terminal so that the top half of the screen still contains some of the previous output.

It seems like a standard (not sure what the name of the standard is) terminal will move the current line to the top of the window when C-l is pressed. Is there a general way to move the current line to the center of the screen? If not, is there a way to do this specifically for iterm2, terminal, or some terminal emulator that has been ported to OS X?

Best Answer

Most terminal emulators can "do" this, but it takes some work:

  • Suppose the line you want to move is at the bottom of the screen.
  • If you want to move that line up, you would scroll (or "index") the line by sending an escape sequence to the bottom row and (counting!) move the line up by the number of lines you want to scroll.
  • Scrolling moves all lines on the screen. You can limit the number of lines which are moved by setting a scrolling region. By doing that, you could keep the lines in the top half of the screen from going away. But lines between the original position of your line and its goal will go away.

That only moves the line up. You can move a line down, similarly, using "reverse indexing". These operations by the way are not in the standard ECMA-48, but are in anything like xterm (based on DEC vt100). ansi-term supports scrolling/indexing.

Finally, that's just up or down. To move a line left/right, you would send escape sequences for inserting or deleting characters while the cursor is at the beginning of the line.

These escape sequences are documented in the terminfo(5) manual page. I've mentioned these:

csr (set scrolling region)
ind (index)
rin (reverse-index)
cup (cursor-position)
ich (insert character)
dch (delete character)

Further reading:

Related Question