Vim Control Sequence Example

# Vim Control Sequence Examples

When a user uses the control sequence, the remote host can control the Tera Term behavior. The Vim procedure for using the control sequence is described below.

## Changing cursor shape on entering and leaving into insert mode

Tera Term can support below control sequence changing the cursor shape.

Abbreviation | Sequence | Function
--- | --- | ---
DECTCEM | ESC [ ? 25 h | Makes the cursor visible
ESC [ ? 25 l | Makes the cursor invisible
DECSCUSR | ESC SP 0 q | Blink Block
ESC SP 1 q | Blink Block
ESC SP 2 q | Steady Block
ESC SP 3 q | Blink Underline
ESC SP 4 q | Steady Underline
ESC SP 5 q | Blink Vertical line
ESC SP 6 q | Steady Vertical line
WYSTCURM | ESC [ 33 h | Steady Wyse Cursor
ESC [ 33 l | Blink Wyse Cursor
WYULCURM | ESC [ 34 h | Steady Wyse underline cursor
ESC [ 34 l | Steady Wyse block cursor
(AT&T 610) | ESC [ ? 12 l | Steady Cursor
ESC [ ? 12 h | Blink Cursor

The vim cursor can be changed in the insert mode to output above control sequences when a user enters(t_SI) and leaves(t_EI).
For example, when below contents is added in the .vimrc file, the cursor style is underline and blinking in the insert mode. Next, the cursor style is block and blinking outside the insert mode.

    let &t_SI .= "\e[3 q"
    let &t_EI .= "\e[1 q"

NOTICE: If a user uses the control sequence except the DECTCEM, turn on the Cursor control sequence of the Additional Settings dialog(The default value is off).

## Auto indent can be disabled on pasting from clipboard

Basically, the host application can not recognize the difference between the user input and pasting from clipboard. However, when a user uses the Bracketed Paste Mode as the xterm extension, the application can recognize its difference and a user can change the behavior of pasting from clipboard.

The vim configuration is described below. The following will use xterm's bracketed paste mode to make pasting automatically enable paste mode and insert mode. Also works fine in ~/.vimrc file.

    if &term =~ "xterm"
        let &t_ti .= "\e[?2004h"
        let &t_te .= "\e[?2004l"
        let &pastetoggle = "\e[201~"

        function XTermPasteBegin(ret)
            set paste
            return a:ret
        endfunction

        noremap <special> <expr> <Esc>[200~ XTermPasteBegin("0i")
        inoremap <special> <expr> <Esc>[200~ XTermPasteBegin("")
        cnoremap <special> <Esc>[200~ <nop>
        cnoremap <special> <Esc>[201~ <nop>
    endif

When a user pastes at the normal mode by using the above configuration, the mode will be automatically changed to the insert mode and do the paste. If this behavior is denied, use the below configuration.
Also, refer to the GNU Screen Notice.

    if &term =~ "xterm"
        let &t_SI .= "\e[?2004h"
        let &t_EI .= "\e[?2004l"
        let &pastetoggle = "\e[201~"

        function XTermPasteBegin(ret)
            set paste
            return a:ret
        endfunction

        inoremap <special> <expr> <Esc>[200~ XTermPasteBegin("")
    endif

#### About Bracketed Paste mode

The bracketed paste mode is the xterm extension feature. When this feature is enabled, the pasted text is bracketed with control sequences so that the program can differentiate the pasted text from typed-in text.
The program will receive: ESC [ 200 ~, followed by the pasted text, followed by ESC [ 201 ~.

## Controlling IME

Tera Term can support the original sequence to control the IME behavior. For example, A user can switch the IME status of enabling and disabling to use this control sequence.

When below contents is be added in the .vimrc file, the IME status is off after the insert mode exiting. Next, the IME status is on after the insert mode enabling.

    let &t_SI .= "\e[<r"
    let &t_EI .= "\e[<s\e[<0t"
    let &t_te .= "\e[<0t\e[<s"

    set timeoutlen=100

When the timeoutlen of the vim is enabled, the vim will wait until either the complete mapping or key sequence has been received. In other words, the timeoutlen is used to describe the time from IME on to off after the ESC key is pressed in the insert mode.
If the timeoutlen is the small value, a trouble may occur that the cursor and function key do not work well.
As an alternative, please use the Delete wait time after ESC key is pushed in insert mode.

## Delete wait time after ESC key is pushed in insert mode

When the ESC key is pressed, Tera Term, xterm and other terminal emulator send the ESC(0x1b) key code. Also, when the cursor key and the function key is pushed, Tera Term sends the ESC key code. Therefore, the host application can not recognize whether the ESC key is pressed.
So, the vim waits for one second when the ESC key code is received to recognize what key is pressed. As a result, when a user presses the ESC key, the time for the insert mode exiting will be late for 1 second. This behavior can not be affected by enabling Changing cursor shape on entering and leaving into insert mode and Controlling IME.

Please use the Application Escape Mode to resolve this problem. When below contents is added in the .vimrc file, the insert mode is quickly exited after the ESC key is pressed.

    let &t_SI .= "\e[?7727h"
    let &t_EI .= "\e[?7727l"
    inoremap <special> <Esc>O[ <Esc>

## Save and Restore Window Title

Tera Term can support the xterm control sequence changing the window title. For example, in case of vim, when the "set title" is executed, the window title will be displayed at the current file name.
However, the vim can change the window title, the vim can not know the old title. So, the vim can not restore the old title, and then the vim will display the "Thanks for flying Vim".

To solve this problem, use the Save and Restore Window Title function. When below configuration is added in the .vimrc file, the vim can save the window title on launching the vim, and then restore the window title on existing the vim.

    let &t_ti .= "\e[22;0t"
    let &t_te .= "\e[23;0t"

## Speed up vsplit scrolling

The vim will slow down by using the vsplit function for a reason of the VT100 and VT200 series specification based on many terminal emulators.

The VT100 specification has the scroll range function(scroll region and vertical margin). For example, when the upper margin is fifth line and the lower margin is twentyth line, the scroll region will be set from fifth to twentyth line. So, a user can not specify the digit unit of the vertical direction.
As a result, the scroll speed will be slow down because the scroll range needs to re-paint all region.

However, the VT400 series specification has the scroll range function(horizontal margin), and then Tera Term 4.79 later has supported this function. When a user configures below settings in the .vimrc file, the vsplit scroll will be speed up.

    if &term =~ "xterm"
        let &t_ti .= "\e[?6h\e[?69h"
        let &t_te .= "\e7\e[?69l\e[?6l\e8"
        let &t_CV = "\e[%i%p1%d;%p2%ds"
        let &t_CS = "y"
    endif

NOTE: The terminal emulators not supporting the horizontal margin can not work the vsplit function by using this configuration. For example, the xterm, mlterm, RLogin, tanasinn and pangoterm terminal emulator can support this function, but other terminal emulators can not support its.

## GNU Screen Notice

When the GNU Screen is used, an application on the screen sends the control sequence and the sequence will be abandoned because the screen can not support the sequence.

    if &term == "screen"
        let &t_SI .= "\eP\e[3 q\e\\"
        let &t_EI .= "\eP\e[1 q\e\\"
    else
        let &t_SI .= "\e[3 q"
        let &t_EI .= "\e[1 q"
    endif

Use the above configuration to resolve this problem. When the terminal type is `screen', the vim entry sandwitches in the sequence between the "\eP" and "\e\\".
However, the screen can not work to switch the window because the sequence can not be managed by the screen.