1

For when you are in Vim's :terminal buffer, and you want to open a new file, but use the existing Vim instance instead of create a new one, Vim has a specific escape sequence you can enter to tell Vim to do a command like open a file:

Job to Vim: JSON API ~
                            *terminal-api*
The job can send JSON to Vim, using a special escape sequence.  The JSON
encodes a command that Vim understands.  Example of such a message:
    <Esc>]51;["drop", "README.md"]<07>

But how do I enter these characters in windows terminal / pwsh?

Looks like I can use the api successfully from zsh (WSL) via the following:

 printf '\033]51;["drop", "README.md"]\07'
xdhmoore
  • 8,935
  • 11
  • 47
  • 90

1 Answers1

1
  • You're seeing an apparent limitation in the native Windows version of Vim (vim.exe) due to the specifics of Windows' VT (Virtual Terminal) support - and the limitation applies irrespective of whether you launch vim.exe stand-alone, from a conhost.exe window (regular console window), or from Windows Terminal:

  • By contrast, the Job-to-Vim escape sequence does work on Unix-like platforms, including when running Vim from inside WSL on Windows (/usr/bin/vim).

The following describes how to emit such an escape sequence from PowerShell in principle, but for the reasons stated, it won't solve your problem in Windows Terminal / Windows console windows.


In PowerShell (Core) 7.3+, from a Unix environment,

printf '\033]51;["drop", "README.md"]\07'

should work as-is.

(In v7.2-, due to a long-standing bug, you'll have to escape the " chars. as \" - see this answer).


The PowerShell-native v7+ equivalent - which works on all platforms - is (see the conceptual about_Special_Characters help topic):

Write-Host -NoNewLine "`e]51;[`"drop`", `"README.md`"]`a"

(In Windows PowerShell, escape sequence `e to produce an ESC char. isn't supported; use $([char] 27) instead.)

Note that "`e]51;[`"drop`", `"README.md`"]`a" alone, using PowerShell's implicit output mechanism, would implicitly print a newline after the string value, hence the use of Write-Host -NoNewLine (alternatively, use [Console]::Write(...))

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • The PowerShell-native v7+ command is close to what I've been trying, but without luck. I'm starting in winterm/pwsh, then `vim`, then `:term`, then executing the above. I'm on pwsh 7.3.4. I've also tried piping it to `out-string` or setting `$PSStyle.OutputRendering = 'ANSI'`, because of [these docs on ANSI escape code support](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_ansi_terminals?view=powershell-7.3) but without luck. I get a blank line printed and nothing from vim. – xdhmoore Jun 25 '23 at 02:43
  • @xdhmoore, does ``[Console]::Write("`e]51;[`"drop`", `"README.md`"]`a")`` make a difference? – mklement0 Jun 25 '23 at 02:56
  • the only difference I see - the command from your original post above leaves an empty line before the next prompt, but the `[Console]` command does not. the prompt prints on the next line. – xdhmoore Jun 25 '23 at 03:21
  • 1
    best I can tell, ignoring unknown OSC commands is [the usual VT100 behavior](https://vt100.net/emu/dec_ansi_parser), but xterm [has a special OSC 51 command for passthru to Emacs](https://invisible-island.net/xterm/ctlseqs/ctlseqs.html), which would explain why vim chose that sequence. I'm not sure I care enough to open an issue for it with win terminal, but I think this is indeed the problem. – xdhmoore Jun 25 '23 at 18:45