在 Windows 上的特定或当前文件夹中启动 WSL Ubuntu
Start WSL Ubuntu in specific or current folder on Windows
在他的开发机器上安装 Linux 和 Ubuntu 的子系统时,我可以通过简单地 [=18= 切换(或启动)到 Ubuntu shell ]
但 Ubuntu shell 默认在 /home/techsupp
文件夹中启动。是否可以强制它在与我使用 Ubuntu
命令的文件夹相同的文件夹中启动?
所以在我的例子中我应该在
/mnt/h
谢谢。
我已经尝试过的:
H:\>ubuntu help
Launches or configures a linux distribution.
Usage:
<no args>
- Launches the distro's default behavior. By default, this launches your default shell.
run <command line>
- Run the given command line in that distro, using the default configuration.
- Everything after `run ` is passed to the linux LaunchProcess call.
config [setting [value]]
- Configure certain settings for this distro.
- Settings are any of the following (by default)
- `--default-user <username>`: Set the default user for this distro to <username>
clean
- Uninstalls the distro. The appx remains on your machine. This can be
useful for "factory resetting" your instance. This removes the linux
filesystem from the disk, but not the app from your PC, so you don't
need to redownload the entire tar.gz again.
help
- Print this usage message.
我也在 uservoice 上发现了这个请求:https://wpdev.uservoice.com/forums/266908-command-prompt-console-windows-subsystem-for-l/suggestions/13421103-let-us-right-click-open-bash-here-from-explorer?tracking_code=8a8bc624c72a8336565fcd6d5737d712
请为它投票。
如果您使用wsl.exe
,它将在当前目录中启动WSL。但 wsl
和 ubuntu
之间的区别在于,如果您安装了两个或更多发行版(例如,Ubuntu 和 Fedora),默认的将启动。您可以通过 运行 wslconfig /setdefault Ubuntu
.
将默认设置为 Ubuntu
参考:[1]https://docs.microsoft.com/en-us/windows/wsl/wsl-config
ubuntu run
run <command line>
Run the provided command line in the current working directory. If no
command line is provided, the default shell is launched.
据我所知,效果与 运行 ubuntu
相同,只是它从当前目录开始。
此命令与在资源管理器中右键单击“在此处打开”命令一样有效。改编自 this article:
- RegEdit 到
HKEY_CLASSES_ROOT\Directory\Background\shell
并创建一个新密钥
- 将
(Default)
编辑为 "Open Ubuntu here" 或类似的。
- 添加一个名为
command
的密钥
- 将
(Default)
编辑为 ubuntu run
现在您在资源管理器中有一个实用的 "Open Ubuntu here" 右键单击菜单。
我对 wsl
的问题是 wsl bash window 没有 Ubuntu 图标,即使它启动了 Ubuntu 分布.
使用这个小脚本,您可以执行 Powershell 当前所在目录中的每个命令。只有当驱动器已经安装到子系统中的 /mnt/ 时,它才有效。它不适用于网络资源。
function tux {$dl = (pwd).Path | wsl eval "cut -c 1 | tr 'A-Z' 'a-z'"; $wd = (pwd).Path | wsl eval "cut -d : -f 2 | tr '\' '/' 2> /dev/null" ; wsl eval "cd '/mnt/$dl$wd' && eval '$args'"}
执行完后,可以这样使用:
tux vim test.txt
我在 Windows 10 Home 上使用 5 月更新并安装了 Ubuntu 18.04 for WSL,我可以在任何文件夹中打开控制台 Shift + 右键单击 并选择 在此处打开 Linux shell 选项
似乎有一个非常简单的解决方案。
只需在 profiles.json 文件的 Ubuntu 配置中添加以下内容
"commandline": "wsl ~ -d Ubuntu",
如果您在任务管理器中查看资源管理器 "Open Linux shell here" 选项如何打开 wsl,您可以看到有一个“--cd”选项。
对我有用的方法是使用 Windows 终端应用程序并将其配置为在所需位置启动。
基于 WSL2 和 Ubuntu20.04 的示例:
settings.json - 将此行添加到“Ubuntu-20.04”部分:
"startingDirectory": "//wsl$/Ubuntu-20.04/home/<username>/"
将 <用户名> 更改为您在安装 Ubuntu 20.04 时创建的用户名。
首先,您必须检查 wsl
是否默认使用 ubuntu。
对我来说不是。在 cmd 中写入以下命令:
wsl -l
Like this
如果不是,则写入 wsl -s Ubuntu-20.04
将其设置为默认值。
现在您可以 运行 ubuntu bash 从任何目录执行 shift + Rightclick 如图所示 Here.
现代解决方案
安装 windows terminal
,来自 Microsoft store
继续设置
安装 ubuntu 后,您将在配置文件列表中看到它,然后单击打开 json
打开cmd
,输入wsl
,输入cd
,输入pwd
wslc,然后复制路径[!
并将其放入 json 文件中,在 ubuntu 个人资料中,startingDirectory
注意: 我使用 linux 主目录作为 ubuntu 使用的默认目录。所以如果你觉得你有其他你想使用的目录,你可以用 /home/claranceliberi
替换它到你自己的首选目录
如果您的默认终端是Ubuntu
,那么您可以在当前目录轻松打开Ubuntu
。
点击文件资源管理器的地址栏
或者您可以使用键盘快捷键 alt+d
和
有类型
wt -d .
如果默认为 Ubuntu
,它将在该路径上打开 Ubuntu
或者您也可以键入
ubuntu run
而且默认终端是否是 Ubuntu
并不重要,它会打开 ubuntu
无论如何。
第 1 步:安装 Windows Terminal
第 2 步:转到要在其中打开 ubuntu 的文件夹,然后右键单击打开 Windows 终端。
第 3 步:转到 Windows 终端设置并导航到您的 Linux 发行版(在我的例子中是 Ubuntu 20.04)并选中此选项。
现在每次您通过 Windows 终端打开 ubuntu 时,它都会在该目录中打开。
在他的开发机器上安装 Linux 和 Ubuntu 的子系统时,我可以通过简单地 [=18= 切换(或启动)到 Ubuntu shell ]
但 Ubuntu shell 默认在 /home/techsupp
文件夹中启动。是否可以强制它在与我使用 Ubuntu
命令的文件夹相同的文件夹中启动?
所以在我的例子中我应该在
/mnt/h
谢谢。
我已经尝试过的:
H:\>ubuntu help
Launches or configures a linux distribution.
Usage:
<no args>
- Launches the distro's default behavior. By default, this launches your default shell.
run <command line>
- Run the given command line in that distro, using the default configuration.
- Everything after `run ` is passed to the linux LaunchProcess call.
config [setting [value]]
- Configure certain settings for this distro.
- Settings are any of the following (by default)
- `--default-user <username>`: Set the default user for this distro to <username>
clean
- Uninstalls the distro. The appx remains on your machine. This can be
useful for "factory resetting" your instance. This removes the linux
filesystem from the disk, but not the app from your PC, so you don't
need to redownload the entire tar.gz again.
help
- Print this usage message.
我也在 uservoice 上发现了这个请求:https://wpdev.uservoice.com/forums/266908-command-prompt-console-windows-subsystem-for-l/suggestions/13421103-let-us-right-click-open-bash-here-from-explorer?tracking_code=8a8bc624c72a8336565fcd6d5737d712
请为它投票。
如果您使用wsl.exe
,它将在当前目录中启动WSL。但 wsl
和 ubuntu
之间的区别在于,如果您安装了两个或更多发行版(例如,Ubuntu 和 Fedora),默认的将启动。您可以通过 运行 wslconfig /setdefault Ubuntu
.
参考:[1]https://docs.microsoft.com/en-us/windows/wsl/wsl-config
ubuntu run
run <command line>
Run the provided command line in the current working directory. If no
command line is provided, the default shell is launched.
据我所知,效果与 运行 ubuntu
相同,只是它从当前目录开始。
此命令与在资源管理器中右键单击“在此处打开”命令一样有效。改编自 this article:
- RegEdit 到
HKEY_CLASSES_ROOT\Directory\Background\shell
并创建一个新密钥 - 将
(Default)
编辑为 "Open Ubuntu here" 或类似的。 - 添加一个名为
command
的密钥
- 将
(Default)
编辑为ubuntu run
现在您在资源管理器中有一个实用的 "Open Ubuntu here" 右键单击菜单。
我对 wsl
的问题是 wsl bash window 没有 Ubuntu 图标,即使它启动了 Ubuntu 分布.
使用这个小脚本,您可以执行 Powershell 当前所在目录中的每个命令。只有当驱动器已经安装到子系统中的 /mnt/ 时,它才有效。它不适用于网络资源。
function tux {$dl = (pwd).Path | wsl eval "cut -c 1 | tr 'A-Z' 'a-z'"; $wd = (pwd).Path | wsl eval "cut -d : -f 2 | tr '\' '/' 2> /dev/null" ; wsl eval "cd '/mnt/$dl$wd' && eval '$args'"}
执行完后,可以这样使用:
tux vim test.txt
我在 Windows 10 Home 上使用 5 月更新并安装了 Ubuntu 18.04 for WSL,我可以在任何文件夹中打开控制台 Shift + 右键单击 并选择 在此处打开 Linux shell 选项
似乎有一个非常简单的解决方案。
只需在 profiles.json 文件的 Ubuntu 配置中添加以下内容
"commandline": "wsl ~ -d Ubuntu",
如果您在任务管理器中查看资源管理器 "Open Linux shell here" 选项如何打开 wsl,您可以看到有一个“--cd”选项。
对我有用的方法是使用 Windows 终端应用程序并将其配置为在所需位置启动。
基于 WSL2 和 Ubuntu20.04 的示例:
settings.json - 将此行添加到“Ubuntu-20.04”部分:
"startingDirectory": "//wsl$/Ubuntu-20.04/home/<username>/"
将 <用户名> 更改为您在安装 Ubuntu 20.04 时创建的用户名。
首先,您必须检查 wsl
是否默认使用 ubuntu。
对我来说不是。在 cmd 中写入以下命令:
wsl -l
Like this
如果不是,则写入 wsl -s Ubuntu-20.04
将其设置为默认值。
现在您可以 运行 ubuntu bash 从任何目录执行 shift + Rightclick 如图所示 Here.
现代解决方案
安装
windows terminal
,来自 Microsoft store继续设置
安装 ubuntu 后,您将在配置文件列表中看到它,然后单击打开 json
打开
cmd
,输入wsl
,输入cd
,输入pwd
wslc,然后复制路径[!并将其放入 json 文件中,在 ubuntu 个人资料中,
startingDirectory
注意: 我使用 linux 主目录作为 ubuntu 使用的默认目录。所以如果你觉得你有其他你想使用的目录,你可以用 /home/claranceliberi
替换它到你自己的首选目录
如果您的默认终端是Ubuntu
,那么您可以在当前目录轻松打开Ubuntu
。
点击文件资源管理器的地址栏
或者您可以使用键盘快捷键 alt+d
和
有类型
wt -d .
如果默认为 Ubuntu
,它将在该路径上打开 Ubuntu
或者您也可以键入
ubuntu run
而且默认终端是否是 Ubuntu
并不重要,它会打开 ubuntu
无论如何。
第 1 步:安装 Windows Terminal
第 2 步:转到要在其中打开 ubuntu 的文件夹,然后右键单击打开 Windows 终端。
第 3 步:转到 Windows 终端设置并导航到您的 Linux 发行版(在我的例子中是 Ubuntu 20.04)并选中此选项。
现在每次您通过 Windows 终端打开 ubuntu 时,它都会在该目录中打开。