Josh Triplett | 29 Dec 10:39 2010

vim -w or -W records K_SPECIAL sequence from xterm version string in script

vim -w or -W records all the keystrokes entered in Vim.  If running on
an xterm or another terminal that supports requesting the terminal
version, vim will do so, and process the resulting version response to
set a few options.  In the process of doing so, it ends up recording
three extra bytes at the beginning of the script file:

/tmp$ vim -u /dev/null -N -W foo
/tmp$ hd foo
00000000  80 fd 35 5a 5a                                    |..5ZZ|

This affected quite a few people playing on

After some adventures through vim's source, I think I've tracked down exactly
what happens to cause the problem:

- Vim asks the terminal for its version string when it starts, in the
  may_req_termresponse function, by sending the kPRV string from the
  terminfo record, and then calling vpeekc_nomap.

- That reads the response from stdin into typebuf, and eventually ends
  up in check_termcode, which handles terminal escape sequences.

- check_termcode finds the version response, and if it manages to
  extract an xterm version number, it sets various features based on
  that version number.

- check_termcode then translates the escape sequence into the key_name
  array as KS_EXTRA and KE_IGNORE, which match the second and third
  bytes of the escape sequence.
(Continue reading)