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 [ 0 SP q Blink Block
ESC [ 1 SP q Blink Block
ESC [ 2 SP q Steady Block
ESC [ 3 SP q Blink Underline
ESC [ 4 SP q Steady Underline
ESC [ 5 SP q Blink Vertical line
ESC [ 6 SP 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 vertical line and blinking in the insert mode, the cursor style is underline and blinking in the replace mode, and the cursor style is block and blinking in the normal mode.

let &t_SI .= "\e[5 q"
let &t_SR .= "\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 option on the Control Sequence tab of [Setup] Additional settings... (The default value is off).

Auto indent can be disabled on pasting from clipboard

NOTE: When text is pasted by using Vim 8.0.0238 or later, auto indent and completion are disabled not below configuration because the vim editor supports Bracketed Paste Mode.
However, when tmux is used, below configuration is needed because Bracketed Paste Mode is not enabled.

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 has("patch-8.0.0238")
    " When below configuration is not set by using Bracketed Paste Mode supporting version(8.0.0238 or later),
    " the Bracketed Paste Mode is used while TERM is xterm.
    " When tmux is used, below configuration is needed because TERM is screen.
    if &term =~ "screen"
        let &t_BE = "\e[?2004h"
        let &t_BD = "\e[?2004l"
        exec "set t_PS=\e[200~"
        exec "set t_PE=\e[201~"
    endif
else
    " The Bracketed Paste Mode motion of Vim core will not work well, 
    " auto indent does not become to disabling, with Vim from 8.0.0210 to 8.0.0237 version.
    " So, Vim core function is disabled.
    if has("patch-8.0.0210")
        set t_BE=
    endif

    " When Vim core does not support Bracketed Paste Mode, below configuration works.
    if &term =~ "xterm" || &term =~ "screen"
        let &t_ti .= "\e[?2004h"
        let &t_te .= "\e[?2004l"

        function XTermPasteBegin(ret)
            set pastetoggle=<Esc>[201~
            set paste
            return a:ret
        endfunction

        noremap <special> <expr> <Esc>[200~ XTermPasteBegin("0i")
        inoremap <special> <expr> <Esc>[200~ XTermPasteBegin("")
        vnoremap <special> <expr> <Esc>[200~ XTermPasteBegin("c")
        cnoremap <special> <Esc>[200~ <nop>
        cnoremap <special> <Esc>[201~ <nop>
    endif
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"

    function XTermPasteBegin(ret)
        set pastetoggle=<Esc>[201~
        set paste
        return a:ret
    endfunction

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

Save and Restore Window Title

NOTE: Since Vim 8.1.0253, Vim supports save and restore window title feature, and following setting isn't necessary.

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.

if !has("patch-8.1.0253")
    let &t_ti .= "\e[22;0t"
    let &t_te .= "\e[23;0t"
endif

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 sandwiches 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.


Vim Configuration

Background color

The default terminal type (TERM) of Tera Term Mac is xterm-256color, so vim knows that 256 colors are available. So, a user must properly configure the background setting to draw the highlight color regarding the vim background color.
When the background color is the dark color(i.e, black), specify the dark setting. Also, when the background color is the bright color(i.e, white), specify the light setting.

set background=dark

With above setting, the vim can know the terminal background color. So, if the configuration is mistaken, the highlight color will be indistinct because the text and the background color is almost same.

Also, refer to the :help 'background' on the vim editor for details.