2

Some background: I usually use Vim on Ubuntu for opening files because you can right click and choose "open with Vim". I have been experimenting with Vim on the Windows subsystem for Linux.

To open a file on the Windows subsystem for Linux the only way seems to be to navigate to the directory using the bash terminal and type vim filename.extension. This is very inefficient for my work flow. Is there any way to get an "open with Vim" option using the Windows Subsystem for Linux?

A.B.
  • 51
  • 1
  • 5
  • In Windows you can set [g]Vim as your default editor as explained here: https://stackoverflow.com/questions/5402615/vim-as-the-default-editor . Then you should be able to open files in Vim by double click on them. – builder-7000 Apr 17 '18 at 22:53
  • Have you considered creating an environment variable for your work area (e.g. $projects) and use an ex command to open the folder with netrw? e.g. `:e $projects`? I use a permutation of this on my windows 10 laptop. – Patrick Bacon Apr 17 '18 at 23:05
  • @Sergio The Windows subsystem for Linux does not allow Linux GUI applications to run, only bash terminal applications such as Vim, Furthermore, I am using Vim in the Windows subsystem for Linux specifically to get access to the bash terminal. I am looking for a way to have a file opened in the Windows subsystem for linux without having to navigate to the directory using the bash terminal. – A.B. Apr 18 '18 at 04:22
  • @Patrick Bacon That sounds like a good workaround to the problem, but still doesn't answer how I can open a file directly from my file system (Either by clicking on it or right clicking and open with). In Linux I can open a file directly into Terminal Vim, so it seems like there should be a workaround for Windows. – A.B. Apr 18 '18 at 04:33
  • Does this answer your question? [Script to enable double click a file in explorer and launch/run it with a WSL App (Neovim, Vim, etc) within Windows Terminal](https://stackoverflow.com/questions/62876681/script-to-enable-double-click-a-file-in-explorer-and-launch-run-it-with-a-wsl-ap) – m4110c Sep 19 '20 at 15:26

2 Answers2

1

After lots of experimentation, I have decided the best approach is to use Vim for windows instead of vim for WSL. The primary reasons being that Vim for WSL does not support copy paste functions from clipboard, and you cannot right-click a file and select open with Vim. To make the open with Vim option work with Windows Vim you have to edit the registry key value in

[HKEY_LOCAL_MACHINE\SOFTWARE\Vim\Gvim]

by changing the path

path=C:\Program Files\Vim\vim74\gvim.exe

to

path=C:\Program Files\Vim\vim74\vim.exe

This solution was found from the post How do I make “Edit with Vim” open Vim instead of gVim on windows?

Update: After even more experimentation Windows Vim does not seem to support the mouse=a command, and copy and paste is really buggy/nonexistent on both WSL and cmd Vim, so it might be better to use GVim or install a Linux distribution.

A.B.
  • 51
  • 1
  • 5
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 '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;
Victor S.
  • 2,510
  • 3
  • 22
  • 35