在资源管理器中双击文件并在Windows终端中使用WSL应用程序(Neovim、Vim等)启动/运行该文件的脚本

oipij1gg  于 2022-11-30  发布在  Windows
关注(0)|答案(2)|浏览(154)

我使用WSL(Windows Subsystem for Linux)。我在WSL中安装了Neovim/Vim/etc,并希望能够在资源管理器中打开一个文件,并在Windows终端中打开安装在WSL中的Neovim。要写什么脚本来启动它,我正在努力从cmd中转义双引号,通过wt.exe,然后进入WSL bash。
保持为Windows和Linux配置的两个版本的Neovim/Vim可能是一件痛苦的事情。而不是在Windows和WSL中只使用WSL版本。

dced5bon

dced5bon1#

TL;DR

将文件类型与运行此批处理脚本相关联(相应地设置myapp):

@echo off
set my_app=nvim
set my_wt_profile="Ubuntu-20.04"
set pp=%1
set pp=%pp:'='\''%
set pp=%pp:;=\;%
set launch="p=$(wslpath '%pp:"=%') && cd \\"^""$(dirname \\"^""$p\\"^"")\\"^"" && %my_app% \\"^""$p\\"^""
start wt.exe new-tab -p %my_wt_profile% bash -i -c %launch%

以及github repo of the scripts

简介

不幸的是,你不能将PowerShell脚本与文件类型相关联(通过open withchoose another appLook for another app on this pc)。选择写一个批处理文件,并把所有的逻辑都放在那里。创建一个bash脚本或文件加载时的vim插件会更容易,但有两个部分的谜题需要彼此同步。
请执行下列步骤:
1.创建一个批处理文件并粘贴以下代码(我们将此脚本称为“启动脚本”,我将其命名为wsl_nvim.bat):
注:以下代码与TL相同; DR版本,但带有注解:

:: This batch script is meant to be associated with file types, such that when
:: the associated file type is opened, it calls this script.
:: This script then open it with Neovim within WSL in a windows terminal (wt).

:: If require a " in the bash command, escape it with \\"^""
:: Example1: To print in bash via cmd the following string: hel'lo
:: bash -i -c "echo "^""hel'lo"^"" "
:: Example2: To print in bash via cmd via wt.exe: hel'lo
:: wt.exe new-tab -p "Command Prompt" cmd /k bash -i -c "echo \\"^""hel'lo\\"^"" "
::
:: To cd to the parent dir: cd "$(dirname "$p")"
:: Escaping it becomes:     cd \"^""$(dirname \"^""$p\"^"")\"^""

@echo off

:: === REQUIRED CUSTOM VALUES ==================================================

:: The name of the WSL app to run
set my_app=nvim

:: The name of your windows terminal linux profile, open the windows terminal
:: settings file and file the linx profile name, e.g.: "name": "Ubuntu-20.04",
set my_wt_profile="Ubuntu-20.04"

:: =============================================================================

:: Windows passes in the filepath in double quotes, e.g.: "C:\Users\Michael\foo.txt"
set pp=%1

:: We passing the path into bash, which has $ and \, so we pass within single quotes
:: so all chars will be taken literally, except the single quote, which we can
:: escape with '\''
set pp=%pp:'='\''%

:: When wt.exe interprets the string, need to escape the semicolon with \;
set pp=%pp:;=\;%

:: Launch basically does: pass in $p, get wslpath of $p, then cd to the dir
:: of the wslpath, then open wslpath with nvim.
:: wslpath requires the input to be within single quotes, or else it will fail.
:: full wt.exe path: %LOCALAPPDATA%\Microsoft\WindowsApps\wt.exe
:: GIANT GOTCHA! Can only strip outter double quotes from %pp% if placing within
:: double quotes, else special chars will be interpreted literally, e.g. ^ will escape.
set launch="p=$(wslpath '%pp:"=%') && cd \\"^""$(dirname \\"^""$p\\"^"")\\"^"" && %my_app% \\"^""$p\\"^""

:: Use `start` to launch cmd and cleanup/close the parent process immediately.
:: bash -i starts bash interactively.
:: bash -c "long command" start bash and allow one to pass in a command to run.
start wt.exe new-tab -p %my_wt_profile% bash -i -c %launch%

奖励1-将“启动脚本”与文本文件类型关联

让我们在explorer.exe的右键单击Open with选项中提供批处理脚本选项。在Open with下,用户可能必须选择choose another app并向下滚动,这仍然比为每个新文件类型搜索文件系统以获取批处理脚本容易得多。
请执行下列步骤:
1.复制以下批处理脚本,并将其粘贴到与上述脚本位于同一目录的文件中。
1.将myscript的值更改为指向您选择的“启动脚本”名称。
1.保存并关闭批处理脚本。
1.右键单击批处理脚本并选择Run as administrator

:: This batch script associates the files in %list% with the `txtfile` type, and
:: changes the `txtfile` type to open with with %myscrip%.
:: It does not make it the default app.
:: One can't programmatically change the default file association of an already
:: associated filetype in Windows 10 after the first login without the gui,
:: this is by design for security.

@echo off
echo !!! THIS SCRIPT MUST BE RUN AS ADMIN !!!

:: === CUSTOM VALUES START =====================================================

:: A space separated list of extensions to be associated with the `txtfile` type
set list=css gitignore html ini js json lua log markdown md php py render sass scss template text txt xml

:: Set myscript to the double quote filepath of the script to run
:: %~dp0 is the dir of this script file
set myscript="%~dp0wsl_nvim.bat"

:: === CUSTOM VALUES END =======================================================

:: e.g. require the same as if one typed into cmd: ftype txtfile="C:\current dir\wsl_nvim.bat" "%1"
echo:
echo Create a `ftype` called `txtfile` and assign it to run with WSL NVIM:"
ftype txtfile=%myscript% "%%1"

echo:
echo `ftype` set for `txtfile`, let's check its set:
ftype | findstr "txtfile"

echo:
echo Create a `assoc` between extensions in %list% with `txtfile`
(for %%a in (%list%) do (
   assoc .%%a=txtfile
))

echo:
echo `assoc` set for each extension, lets check `assoc`:
assoc | findstr ".txt"

echo:
echo Now if you right click on one of these file extensions, and select `Open with`,
echo and select `choose another app`, it should list %myscript% there.
echo SCRIPT COMPLETE.
pause

奖励2-创建任务栏快捷方式

1.复制“启动脚本”路径。
1.在资源管理器中,右键单击NewShortcut
1.键入合适的名称,例如WSL NVIM
1.现在,如果右键单击新创建的快捷方式,***没有***选项可以固定到任务栏或启动。
1.右键单击快捷方式,选择Properties并将target字段更改为(确保自定义***两个***路径!):

cmd.exe /s /c ""C:\path\to\launch\script\wsl_nvim.bat" "\\wsl$\Ubuntu-20.04\desirable\default\location\temp_filename""

对我来说,上面的内容看起来像:

cmd.exe /s /c ""C:\code\software_setup\utils\wsl_nvim.bat" "\\wsl$\Ubuntu-20.04\home\michael\temp""

现在如果你双击快捷方式,它应该会在给定的位置打开一个空文件,文件名为temp(取决于你的应用程序如何处理路径)。现在你可以右键单击快捷方式,并将其固定到任务栏或开始菜单。

v09wglhw

v09wglhw2#

如果我没理解错你的问题,你想在Windows中使用WSL Vim编辑文件。下面的方法在你的(右键单击)上下文菜单中创建一个条目Open in Vim,你可以单击它,在WSL-Vim中打开任何文件。当然,你可以通过修改这个过程来执行任何其他可以在终端窗口中运行的程序(例如bash)。
1.打开regedit.exe并导航至HKEY_CLASSES_ROOT\*\shell
1.右键单击shell并选择New->Key,将其命名为Vim
1.双击(Default)并将值设置为Open with Vim
1.右键单击Vim-Key并选择New->Key,将其命名为command
1.(左)单击新创建的command-Key,然后双击(Default)(右窗格)
1.在Value Data下输入:C:\Windows\System32\bash.exe -c "vim \"$(wslpath '%1')\""

(在此步骤中,您可以使用任何其他可以在bash中运行的程序替换vim。请记住,您可能需要-i标志才能获得交互式环境)

它应该如下所示:

现在,如果您在Windows中右键单击任何文档,您应该会看到“Open with Vim”。
如果还希望Vim图标出现在上下文菜单中,请执行以下操作:
1.从此处下载Vim图标:https://raw.githubusercontent.com/vim/vim/master/src/vim.ico
1.将其存储在磁盘上的某个位置(我将其存储在C:\Users\<USERNAME>\Pictures\Saved Pictures\vim.ico下)
1.打开regedit.exe并导航至HKEY_CLASSES_ROOT\*\shell
1.右键单击Vim并选择New->String Value,将其命名为Icon
1.双击新创建的Icon-条目,然后在Value Data下输入vim.ico的路径。
它应该如下所示:

而这就是最后的结局:

相关问题