用于在资源管理器中双击文件并在Windows Terminal中使用WSL应用程序(Neovim,Vim等)启动/运行该文件的脚本 [英] Script to enable double click a file in explorer and launch/run it with a WSL App (Neovim, Vim, etc) within Windows Terminal

查看:93
本文介绍了用于在资源管理器中双击文件并在Windows Terminal中使用WSL应用程序(Neovim,Vim等)启动/运行该文件的脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用WSL(Linux的Windows子系统).我在WSL中安装了Neovim/Vim/etc,并希望能够在资源管理器中打开文件,并说在Windows终端中的WSL中​​安装的Neovim中将其打开.一个脚本是用什么脚本来启动的,我一直在努力将 cmd 中的双引号转义,通过wt.exe,然后转为WSL bash.

保留两个为Windows和Linux配置的Neovim/Vim版本可能会很痛苦.而是在Windows和WSL中仅使用WSL版本.

解决方案

TL; DR

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

  @echo关闭设置my_app = nvim设置my_wt_profile ="Ubuntu-20.04"设置pp =%1设置pp =%pp:'='\''%设置pp =%pp:; = \;%设置启动="p = $(wslpath'%pp:'=%')&&cd \\"^""$(dirname \\" ^""$ p \\" ^")\\"^"&&%my_app%\\"^""$ p \\" ^启动wt.exe新选项卡-p%my_wt_profile%bash -i -c%launch% 

脚本的github回购

简介

不幸的是,一个人无法将Powershell脚本与文件类型相关联(通过打开方式选择另一个应用程序在此PC上查找另一个应用程序).选择编写一个批处理文件,然后将所有逻辑放入其中.在文件加载时创建bash脚本或vim插件本来会更容易,但是那么难题中有2个部分需要相互配合.

执行以下步骤:

  1. 创建一个批处理文件并粘贴以下代码(我们将该脚本称为启动脚本",我将其命名为 wsl_nvim.bat ):

注意:以下代码与TL; DR版本相同,但带有注释:

  ::该批处理脚本与文件类型相关联,例如::关联的文件类型已打开,它将调用此脚本.:::此脚本然后在Windows终端(wt)中的WSL中​​用Neovim打开它.::如果需要在bash命令中,使用\\"^"进行转义.:: Example1:通过cmd在bash中打印以下字符串:hel'lo:: bash -i -c"echo" ^" hel'lo"^""":: Example2:通过wt.exe通过cmd在bash中打印:hel'lo:: wt.exe new-tab -p命令提示符";cmd/k bash -i -c"echo \\" ^" hel'lo \\"^""::::到cd到父目录:cd``$(dirname"$ p"))::转义它成为:cd \"^""$(dirname \" ^" $ p \"^")\" ^@回声关闭:: ===所需的自定义值==================================================::要运行的WSL应用程序的名称设置my_app = nvim::您的Windows终端机Linux配置文件的名称,打开Windows终端机::设置文件并归档linx配置文件名称,例如:名称":"Ubuntu-20.04",设置my_wt_profile ="Ubuntu-20.04":: ===========================================================================:: Windows在文件路径中使用双引号将其传递,例如:"C:\ Users \ Michael \ foo.txt";设置pp =%1::我们将路径传递到具有$和\的bash中,因此我们在单引号内传递::,因此所有字符都将按字面意义使用,但单引号除外,我们可以::以'\''逃脱设置pp =%pp:'='\''%::当wt.exe解释字符串时,需要用\转义分号;设置pp =%pp:; = \;%::启动基本上可以做到:传入$ p,获取$ p的wslpath,然后将cd转到dirwslpath的::,然后用nvim打开wslpath.:: wslpath要求输入必须用单引号引起来,否则它将失败.::完整的wt.exe路径:%LOCALAPPDATA%\ Microsoft \ WindowsApps \ wt.exe::巨人哥达!如果放置在%pp%中,则只能去除%pp%的双引号::双引号,否则特殊字符将按字面意义进行解释,例如^会逃脱.设置启动="p = $(wslpath'%pp:'=%')&&cd \\"^""$(dirname \\" ^""$ p \\" ^")\\"^"&&%my_app%\\"^""$ p \\" ^::使用开始"启动cmd并立即清理/关闭父进程.:: bash -i以交互方式启动bash.:: bash -c长命令"启动bash并允许其传递命令以运行.启动wt.exe新选项卡-p%my_wt_profile%bash -i -c%launch% 

奖金1-关联启动脚本"文本文件类型

让批处理脚本作为资源管理器中的一个选项可用.右键单击 Open with 选项.在 Open with 下,可能必须选择 choose another app 并向下滚动,这比为每种新文件类型为批处理脚本寻找文件系统要容易得多.

执行以下步骤:

  1. 复制以下批处理脚本并将其粘贴到与上述脚本相同的目录下的文件中.
  2. 更改 myscript 的值,使其指向启动脚本";您选择的名字.
  3. 保存并关闭批处理脚本.
  4. 右键单击批处理脚本,然后选择以管理员身份运行

  ::该批处理脚本将%list%中的文件与`txtfile`类型相关联,并且::将txtfile类型更改为以%myscrip%打开.::它没有使其成为默认应用程序.::无法以编程方式更改已存在的默认文件关联Windows 10中没有gui的首次登录后的::相关文件类型,::这是出于安全考虑而设计的.@回声关闭回声!此脚本必须以管理员身份运行!!!:: ===定制值开始=====================================================::与"txtfile"类型相关联的扩展名用空格分隔的列表设置list = css gitignore html ini js json lua日志markdown md php py渲染sass scss模板文本txt xml::将myscript设置为要运行的脚本的双引号文件路径::%〜dp0是此脚本文件的目录设置myscript =%〜dp0wsl_nvim.bat":: ===自定义值结束=======================================================::例如要求与键入cmd的要求相同:ftype txtfile =" C:\ current dir \ wsl_nvim.bat"%1"回声:echo创建一个名为"txtfile"的"ftype",并将其分配为与WSL NVIM一起运行.ftype txtfile =%myscript%"%% 1"回声:回显为txtfile设置的ftype,让我们检查一下它的设置:ftype |findstr"txtfile"回声:echo使用txtfile在%list%的扩展名之间创建一个`assoc`(对于(%list%)中的%% a,请执行(关联.%% a = txtfile))回声:为每个扩展设置echo`assoc`,让我们检查`assoc`:assoc |findstr".txt"回声:回声现在,如果您右键单击这些文件扩展名之一,然后选择打开方式",回声并选择选择另一个应用程序",它应该在那里列出%myscript%.回显脚本完成.暂停 

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

  1. 复制启动脚本"路径.
  2. 在资源管理器中,右键单击 New Shortcut .
  3. 输入适当的名称,例如 WSL NVIM
  4. 现在,如果右键单击新创建的快捷方式,则可以使用 选项固定到任务栏或启动.
  5. 右键单击该快捷方式,选择 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" 

现在,如果双击快捷方式,它将打开wsl应用程序,该应用程序在给定位置带有空白文件,并带有临时文件名(取决于应用程序处理路径的方式).现在,您可以右键单击该快捷方式,然后将其固定到任务栏或开始菜单.

I use WSL (Windows Subsystem for Linux). I have Neovim/Vim/etc installed in WSL, and wish to be able to open a file in explorer, and have it open in say Neovim installed in WSL, within windows terminal. What script does one write to launch it, I am struggling with escaping double quotes from cmd, through wt.exe, then into WSL bash.

It can be a pain keeping two versions of Neovim/Vim configured for windows and for linux. Rather just use only the WSL version in both windows and WSL.

解决方案

TL;DR

Associate the file type with running this batch script (set myapp accordingly):

@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%

And github repo of the scripts

Intro

Unfortunately one cannot associate a powershell script with file type (via open withchoose another appLook for another app on this pc). Choose to write a batch file, and put all the logic in there. It would have been easier to create a bash script, or vim plugin on file load, but then there are 2 parts of the puzzle which need to be in syn with each other.

Perform the following steps:

  1. Create a batch file and paste in the following code (we will refer to this script as the "launch script", I named it wsl_nvim.bat):

Note: The following code is the same as the TL;DR version but with comments:

:: This batch script is ment 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 interpretted 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%

Bonus 1 - Associate "launch script" with text file types

Let's make the batch script available as an option in the explorer.exe's right click Open with options. Under Open with one may have to select choose another app and scroll down, which is still much easier than hunting the filesystem for the batch script for each new file type.

Perform the following steps:

  1. Copy the following batch script and paste it into a file in the same dir as the above script.
  2. Change the value of myscript to point to the "launch script" name you choose.
  3. Save and close the batch script.
  4. Right click on the batch script and select 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

Bonus 2 - Create a Taskbar shortcut

  1. Copy the "launch script" path.
  2. In explorer right click NewShortcut.
  3. Type in a suitable name, e.g. WSL NVIM
  4. Now if one right clicks on the newly created shortcut, there is no option to pin to taskbar or start.
  5. Right click on the shortcut, select Properties and change the target field to (make sure to customise both paths!):

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

    For me the above looks like :

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

Now if you double click the shortcut, it should open the wsl app with a blank file in the given location, with temp file name (depending on how your app handles paths). You can now right click on the shortcut and pin it to the taskbar or start menu.

这篇关于用于在资源管理器中双击文件并在Windows Terminal中使用WSL应用程序(Neovim,Vim等)启动/运行该文件的脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆