2

I want to use Vim as my default text editor on Windows 10 (not gVim but the terminal version I installed on wsl). I already made the following setting via cmd.exe:

>ftype txtfile
txtfile=wt ubuntu1804.exe run vim %1

Now, whenever I click on a textfile vim opens inside wsl, but vim creates an empty buffer, as the path got jumbled up: For instance, instead of "C:\Users\Dominik\myfile", the buffer title reads "C:UsersDominikmyfile". How do I give the right path name to vim?

1
  • You might have to put double backslashes ("\\") instead of just one.
    – Mailbox
    Apr 28 at 14:52

1 Answer 1

1

Unfortunately (for me, not for you), ftype/assoc are fairly deprecated, especially on Windows 11 it seems. Other registry entries override any attempts to change the default behavior via the venerable ftype command. I'm happy to hear that they are still working for you, but be aware that you may need to transition to other techniques in the future.

So, since I can't use ftype myself, I can't test out this solution, but I'm gong to propose, as a first pass:

ftype txtfile=wsl -e sh -c "vi \"$(wslpath '%1')\""

Hopefully I'm getting the quoting/escaping correct. I tested it with a hardcoded Windows path (with spaces) from within CMD, at least.

This version:

  • Uses wsl instead of ubuntu. wsl.exe is the replacement command for launching WSL instances (of any distribution) and is much more flexible than the <distribution>.exe versions.

  • Starts WSL by executing (-e) a sh instance with the actual commandline (-c) we need, which ...

  • Uses the wslpath command to translate the Windows-style path that is passed in to the WSL equivalent. For instance, launching C:\readme.txt this way will allow Vim to open /mnt/C/readme.txt.

  • Note that no profile or rc (startup) scripts are read from your shell when launching this way, so be mindful of that if there are any environment variable definitions you set there (or aliases, etc.) when inside Vim. This could bite you if you shell-out from Vim to bash often. Otherwise, it's probably not an issue. There are tweaks that you can do to the command line if that is an issue, but it does make launch slightly longer.

If, in the future you find that ftype no longer works for you, see this answer that I provided just two days ago on an alterative. At that point, I'd totally forgotten about ftype myself until your question reminded me.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .