3

I'd like to add a Windows right-click menu entry to quickly open text files in WSL Vim, without manually opening up a terminal. Thanks @romainl.

davidanderle
  • 638
  • 6
  • 12

2 Answers2

2
  1. Create a registry key at Computer\HKEY_CLASSES_ROOT\*\shell named Open in Vim, and a subkey for it as command:

    Computer\HKEY_CLASSES_ROOT\*\shell\Open in Vim

    Computer\HKEY_CLASSES_ROOT\*\shell\Open in Vim\command

  2. Edit the string value of the command\(Default) as bash.exe -c "wslpath '%1' | xargs nvim" (alternatively, you can substitute nvim with vim).

  3. Download a Vim logo which you can convert online to an .ico extension. Create an Icon string value under the Open in Vim key, and edit its value with the path of the .ico image.

You can see the resulting entry below. Clicking on it would open up a WSL terminal with the selected textfile opened in Vim. Upon exitting Vim, the terminal closes, too. enter image description here

davidanderle
  • 638
  • 6
  • 12
  • This adds a context menu for all filetypes. Is there a way to add a context menu only for textfiles? – Joe Dec 16 '18 at 14:35
  • [@Joe](https://stackoverflow.com/users/5751969/joe) in that case you wanna go create this key structure and do the above described. For me the the \.txt key was completely empty, so I had to create the shell key, too. `Computer\HKEY_CLASSES_ROOT\SystemFileAssociations\.txt\shell\Open in Vim\command` – davidanderle Jan 26 '19 at 11:11
0

Add an item to the context menu to open a file in WSL VIM with PowerShell:

New-Item -Path 'Registry::HKEY_CLASSES_ROOT\*\shell\VIM\command' -Force | Out-Null;
Set-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\`*\shell\VIM' -Name '(default)' -Value 'Open in VIM' -Force | Out-Null;
Set-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\`*\shell\VIM' -Name 'Icon' -Value 'C:\Windows\System32\wsl.exe,0' -Force | Out-Null;
Set-ItemProperty -Path 'Registry::HKEY_CLASSES_ROOT\`*\shell\VIM\command' -Name '(default)' -Value "wsl vim -p `"`$(wslpath '%1')`"" -Force | Out-Null;

Or use a registry file as an alternative.

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\VIM\command]
@="wsl sudo vim -p \"$(wslpath '%1')\""
Victor S.
  • 2,510
  • 3
  • 22
  • 35