Search code examples
terminalptyxtermjs

xtermjs, few questions regaring the usage


Struggling to get using xtermjs, and have some questions which aren't covered in the official documentation, at least I didn't find.

  1. I understand that when I use some app within the terminal, for example, Vim terminal need to be switched to alternate buffer, after I quit from the app, terminal switched back to the normal buffer. Is this right?

  1. To switch between buffers (and overall to control terminal behavior) I need to use a control sequence. It isn't something unique to xterm.js, but the common pattern and control sequence is unified between terminals?

  1. Control sequence to switch to the alternate buffer is CSI ? Pm h with parameter 47 according to documentation:

DECSET DEC Private Set Mode CSI ? Pm h Set various terminal attributes.

Where

paramAction 47 - Use Alternate Screen Buffer.


  1. How to use this control sequence with xterm.js, for example, I want to switch to alternate buffer. What string should be used in terminal.write(...)?

Solution

    1. Yes, see description here in this question Using the “alternate screen” in a bash script

      The alternate screen is used by many "user-interactive" terminal applications like vim, htop, screen, alsamixer, less, ... It is like a different buffer of the terminal content, which disappears when the application exits, so the whole terminal gets restored and it looks like the application hasn't output anything

    2. Yes, ANSI escape code

      ANSI escape sequences are a standard for in-band signaling to control the cursor location, color, and other options on video text terminals and terminal emulators. Certain sequences of bytes, most starting with Esc (ASCII character 27) and '[', are embedded into the text, which the terminal looks for and interprets as commands, not as character codes.

      • Control sequence to switch to alternate buffer: CSI ? 47 h
      • Control sequence to switch to regular buffer: CSI ? 47 l
    3. Code to apply control sequence to switch to alternate buffer:

    terminal.write("\x9B?47h"); //CSI ? 47 h