gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:04:26 25 4
gpt4 key购买 nike

我使用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 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插件本来会更容易,但是那么难题中有2个部分需要相互配合。
执行以下步骤:
  • 创建一个批处理文件并粘贴以下代码(我们将此脚本称为“启动脚本”,我将其命名为wsl_nvim.bat):

  • 注意:以下代码与TL; DR版本相同,但带有注释:
    :: 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%
    奖金1-将“启动脚本”与文本文件类型相关联
    让我们将批处理脚本作为explorer.exe的右键单击 Open with选项中的一个选项提供。在 Open with下,可能必须选择 choose another app并向下滚动,这比为每种新文件类型在批处理脚本中搜索文件系统要容易得多。
    执行以下步骤:
  • 复制以下批处理脚本并将其粘贴到与上述脚本相同的目录中的文件中。
  • 更改myscript的值以指向您选择的“启动脚本”名称。
  • 保存并关闭批处理脚本。
  • 右键单击批处理脚本,然后选择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-创建任务栏快捷方式
  • 复制“启动脚本”路径。
  • 在资源管理器中,右键单击NewShortcut
  • 输入适当的名称,例如WSL NVIM
  • 现在,如果右键单击新创建的快捷方式,则没有没有选项可固定到任务栏或启动。
  • 右键单击快捷方式,选择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应用程序,该应用程序在给定位置带有空白文件,并带有临时文件名(取决于您的应用程序处理路径的方式)。现在,您可以右键单击该快捷方式,然后将其固定到任务栏或开始菜单。

    关于windows - 用于在资源管理器中双击文件并在Windows Terminal中使用WSL应用程序(Neovim,Vim等)启动/运行该文件的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62876681/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com