6

According to the :help startup-options, this is -w's behviour

-w {scriptout}  All the characters that you type are recorded in the file
                "scriptout", until you exit Vim.  This is useful if you want
                to create a script file to be used with "vim -s" or
                ":source!".

Specifically, it mentions the -s flag as a useful application of -w's output.

Testing this, it seems there isn't a 1-1 mapping of the two flags.

For all standard keycodes, -s behaves as expected when running -w's output.

However, when "special" keycodes are used when running with -w (e.g. <Del>, <Up>, ...), Vim will output a special abstraction of these keycodes, that according to the documentation is a terminal-independent representation - for example, <Del> is written in -w's output as <80><6b><44> (hexa) (<80>kD).

I expected Vim's behaviour to interpret the above sequence (<80><6b><44>) as <Del> when it is part of -s's input, but that is not the case. It appears to do something different which I have not been able to fully decipher yet.

Am I trying to use -s in ways that are not intended? Or perhaps there is some hidden Vim option to make this work? Or is it simply a bug in Vim?

Any input will be greatly appreciated, thanks!

Update

Some details I gathered:

  • <80> is interpreted as a literal <80> through -s, which ends up as <80><fe>X. I don't think this is the intended behaviour.
  • The suggestion to use execute 'normal!' ended up not being suited for my use case as macros are disabled when running through normal.

My use case actually required "replaying" user input for a fork of a python implementation of the vimgolf client :) (https://github.com/dankilman/vimgolf).

I ended up using feedkeys("<key sequence>", 't') which met all my requirements.

  • Interesting. Meanwhile the Vim Golf client app actually prettifies -w output so you'll get <Del>. I wonder if putting that through vim -s would result in expected behavior. – B Layer Dec 23 '19 at 22:25
  • Note also the fact that the Vimgolf client has to do the prettying surely indicates that there's no way to get such a result from Vim natively. – B Layer Dec 23 '19 at 22:33
  • 1
    Thanks for the link @BLayer. Putting <Del> through -s will actually interpret it as the actual keys: <, D, e, l, > :) – dankilman Dec 23 '19 at 23:38
  • Well, that's no good. I guess it will come as no surprise that I've used -s exactly zero times in my life. :D I was thinking it parsed along the lines of mapping and/or expression parser. :P – B Layer Dec 24 '19 at 3:09
  • 2
    I expected Vim's behaviour to interpret the above sequence (<80><6b><44>) as <Del> Looks like source! fully expects only text input. But note that this seems to work okay: :execute 'normal!' readfile('scriptout', 'b')[0] – Matt Dec 24 '19 at 5:30
1

This is a bug registered on March 17th, 2019 and reported on February 24th, 2019. From :help known-bugs:

Bug: script written with "-W scriptout" contains Key codes, while the script
read with "-s scriptin" expects escape codes.  Probably "scriptout" needs to
be adjusted. (Daniel Steinberg, 2019 Feb 24, #4041)

As of this writing, it doesn't have a priority classification so it might take a while before we get a fix for it. A similar issue reported on October 20th, 2010 (and related issue on March 4th, 2013) suggests this bug is in place (at least) since then.

|improve this answer|||||
  • 1
    An interesting anecdote is that the person that filed this bug is the same person with the initial python vimgolf client implementation which I based my fork on :) – dankilman Dec 26 '19 at 22:51
  • Oh, I didn't know that. What a small world we live in. – Jair López Dec 26 '19 at 23:31
1

It looks like a nasty bug.

When writing a script out, Vim uses its "<>-notation" for keycodes, i.e. "\<BS>" (<80>kb) for backspace key, "\<del>" (<80>kD) for delete key, etc.

However, they only get converted back into "real" keys if pumped through :normal. While -s or :source! simply allow them to pass through!

It's also interesting to note that Neovim seems to suffer from the same problem, except "backspace" key, as it gets written out as <8>, so it eventually works. But "\<del>" and others are just the same as in Vim.

So as a workaround you can do the following: :execute 'normal!' readfile('scriptout', 'b')[0] Otherwise, I believe, it should be reported as a bug.

|improve this answer|||||
  • 1
    It should be noted that using this (:execute 'normal!') method, if scriptout included any recording of macros, they will not be recorded when replayed. – dankilman Dec 26 '19 at 15:54
  • 1
    @dankilman q does not work in :normal, as it's mentioned in :h q. And also all other limitations would apply, such as "can't stay in insert mode", "can't gQ to ex-mode" etc. – Matt Dec 26 '19 at 16:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.