简体   繁体   English

在 Windows 上的特定或当前文件夹中启动 WSL Ubuntu

[英]Start WSL Ubuntu in specific or current folder on Windows

When installing Subsystem for Linux and Ubuntu from store on his development machine I can switch (or start) to Ubuntu shell by simply在他的开发机器上从商店安装 Linux 和 Ubuntu 子系统时,我可以简单地切换(或启动)到 Ubuntu shell

在此处输入图片说明

But the Ubuntu shell start in /home/techsupp folder by default.但是 Ubuntu shell 默认在/home/techsupp文件夹中启动。 Is it possible to force it to start in same folder than the one I use my Ubuntu command?是否可以强制它在与我使用Ubuntu命令的文件夹相同的文件夹中启动?

So in my example I should be in所以在我的例子中,我应该在

/mnt/h

Thank you.谢谢你。

What I already tried:我已经尝试过的:

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.

I also discover this request on 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我也在 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

Please vote for it.请为它投票。

I'm on Windows 10 Home with May Update and have Ubunto 18.04 for WSL installed, I can open the console in any folder with Shift + Right Click and selecting the Open Linux shell here option我在带有 May Update 的 Windows 10 Home 上安装了用于 WSL 的 Ubunto 18.04,我可以使用Shift + 右键单击并选择在此处打开 Linux shell选项在任何文件夹中打开控制台

在此处输入图片说明

如果您在任务管理器中检查资源管理器“在此处打开 Linux shell”选项如何打开 wsl,您可以看到有一个“--cd”选项。

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.提供命令行,启动默认的shell。

As far as I can tell the effect is the same as running ubuntu , except that it starts in the current directory.据我所知,效果与运行ubuntu相同,只是它在当前目录中启动。

This command works even as a right click open here command in explorer.该命令甚至可以作为资源管理器中的右键单击在此处打开命令。 Adapting from this article :改编自这篇文章

  • RegEdit to HKEY_CLASSES_ROOT\\Directory\\Background\\shell and create a new key RegEdit 到HKEY_CLASSES_ROOT\\Directory\\Background\\shell并创建一个新密钥
  • edit (Default) to "Open Ubuntu here" or similar.编辑(Default)为“在此处打开 Ubuntu”或类似内容。
  • add a key named command添加一个名为command的键
  • edit (Default) to ubuntu run编辑(Default)ubuntu run

Now you have a functional "Open Ubuntu here" right click menu in explorer.现在您在资源管理器中有一个功能齐全的“在此处打开 Ubuntu”右键菜单。


The problem I have with wsl is that the wsl bash window doesn't have the Ubuntu icon, even if it starts the Ubuntu distribution.我对wsl的问题是 wsl bash 窗口没有 Ubuntu 图标,即使它启动了 Ubuntu 发行版。

If you use wsl.exe , it will start WSL in current directory.如果您使用wsl.exe ,它将在当前目录中启动 WSL。 But the difference between wsl and ubuntu is that if you have installed two or more distros (e,g, Ubuntu and Fedora), the default one will get started.但是wslubuntu之间的区别在于,如果您安装了两个或多个发行版(例如,Ubuntu 和 Fedora),则会启动默认的发行版。 You can set the default to Ubuntu by running wslconfig /setdefault Ubuntu .您可以通过运行wslconfig /setdefault Ubuntu将默认设置设置为wslconfig /setdefault Ubuntu

Reference: [1] https://docs.microsoft.com/en-us/windows/wsl/wsl-config参考:[1] https://docs.microsoft.com/en-us/windows/wsl/wsl-config

seems that there is a very simpel solution.似乎有一个非常简单的解决方案。

just add the following in Ubuntu configuration in the profiles.json file只需在profiles.json文件中的Ubuntu配置中添加以下内容

"commandline": "wsl ~ -d Ubuntu", "命令行": "wsl ~ -d Ubuntu",

The method that has worked for me is to use the Windows Terminal app and configure it to start in the desired location.对我有用的方法是使用 Windows 终端应用程序并将其配置为在所需位置启动。

Example based on WSL2 and Ubuntu 20.04:基于 WSL2 和 Ubuntu 20.04 的示例:

settings.json - Add this line to "Ubuntu-20.04" section: settings.json - 将此行添加到“Ubuntu-20.04”部分:

    "startingDirectory": "//wsl$/Ubuntu-20.04/home/<username>/"

Change <username> to be the name of the user you created on install of Ubuntu 20.04.将 <username> 更改为您在安装 Ubuntu 20.04 时创建的用户名。

with this little script you can execute every command from the directory your Powershell is currently in. It only works if the drive is already mounted to /mnt/ in the subsystem.使用这个小脚本,您可以执行 Powershell 当前所在目录中的每个命令。它仅在驱动器已安装到子系统中的 /mnt/ 时才有效。 It doesn't work with network resources.它不适用于网络资源。

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'"}

after you have executed it, you can use it like this:执行后,您可以像这样使用它:

tux vim test.txt

First, you have to check whether wsl is using ubuntu as default or not.首先,您必须检查wsl是否默认使用 ubuntu。 For me it was not.对我来说不是。 Write the below command on cmd:在cmd上写如下命令:

wsl -l

Like this像这样

If not then Write wsl -s Ubuntu-20.04 to set it as default.如果没有,则编写wsl -s Ubuntu-20.04将其设置为默认值。 Now you can run ubuntu bash from any directory by doing shift + Rightclick as shown Here .现在你可以运行Ubuntu由如图所示做SHIFT +右击从任何目录猛击这里

Modern solution现代解决方案

  1. Install windows terminal , from Microsoft storeMicrosoft Store安装windows terminal

  2. Go on settings继续设置在此处输入图片说明

  3. Once you have ubuntu installed you will see it on the list of profiles and then click on open json安装 ubuntu 后,您将在配置文件列表中看到它,然后单击打开 json 在此处输入图片说明

  4. Open cmd , type wsl , type cd , type pwd wslc then copy the path [!打开cmd ,输入wsl ,输入cd ,输入pwd wslc 然后复制路径 [! 在此处输入图片说明

  5. And put it in the json file, on ubuntu profile, startingDirectory并将其放入 json 文件中,在 ubuntu 配置文件中, startingDirectory 在此处输入图片说明

Note: I have used linux home directory as default directory used by ubuntu.注意:我使用 linux 主目录作为 ubuntu 使用的默认目录。 So if you feel like you have other directory you want to use you can replace it with /home/claranceliberi to your own prefereble directory因此,如果您觉得还有其他要使用的目录,可以将其替换为/home/claranceliberi到您自己的首选目录

if your default terminal is Ubuntu , then you can open Ubuntu in the current dir easily.如果您的默认终端是Ubuntu ,那么您可以轻松地在当前目录中打开Ubuntu

Go to the address bar of the file explorer by clicking on it单击它转到文件资源管理器的地址栏在此处输入图片说明

or you can use keyboard shortcut alt+d and或者您可以使用键盘快捷键alt+d

there type有类型

wt -d .

and if the default is Ubuntu , it will open Ubuntu on that path or you can also type如果默认是Ubuntu ,它会在那个路径上打开Ubuntu或者你也可以输入

ubuntu run 

and it doesn't matter if the default terminal is Ubuntu or not, it will open ubuntu there regardless of anything.并且默认终端是否为Ubuntu并不重要,它会在那里打开ubuntu ,无论如何。

Step 1: InstallWindows Terminal第 1 步:安装Windows 终端

Step 2: Go to the folder you want to open ubuntu in and open Windows Terminal by right-clicking.第 2 步:转到要在其中打开 ubuntu 的文件夹,然后右键单击打开 Windows 终端。

Step 3: Go to Windows Terminal Settings and navigate to your Linux distro (in my case Ubuntu 20.04) and check this option.第 3 步:转到 Windows 终端设置并导航到您的 Linux 发行版(在我的情况下为 Ubuntu 20.04)并选中此选项。

在此处输入图片说明

Now every time you open ubuntu through the Windows terminal it will open in that directory.现在,每次您通过 Windows 终端打开 ubuntu 时,它都会在该目录中打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM