如何在 Android Studio 终端中使用 cygwin bash 并在项目目录中启动它?

How to use cygwin bash in Android Studio terminal and start it at project directory?

this blog post 开始,您可以在 Settings -> Tools -> Terminal -> Shell path 下将 Android Studio 终端设置为 cygwin/bash。但是,新终端将从您的主目录 ($HOME) 开始。您可以按照博客 post 中的建议将 cd $OLDPWD 放入 .bashrc,但是,这会扰乱您的正常 cygwin 会话。 (将从 /cygdrive/c/Windows/system32 开始)

是否可以在项目目录中启动 Android Studio 终端,而正常的 cygwin 会话将在 $HOME 启动?

两步

  1. 创建一个 Cygwin-AndroidStudio.bat 批处理文件来设置自定义变量。我选择IDE。将 Android StudioShell path 设置为

    "cmd.exe" /c "{path to}/Cygwin-AndroidStudio.bat"
    
  2. 将以下脚本放入Cygwin-AndroidStudio.bat

    @echo off
    set IDE=AndroidStudio
    C:\{path to}\cygwin64\bin\bash --login -i
    
  3. 将以下内容添加到`cygwin

    下的~/.bashrc末尾
    if [ ! -z "${IDE}" -a "${IDE}" == "AndroidStudio" ]; then
        cd $OLDPWD;
    fi
    

说明

  1. 自 2016 年 8 月 30 日起,您需要 quote both cmd.exe and the path to the script.
  2. 它首先设置一个window变量IDEAndroidStudio。 (这是一个任意字符串)。之所以有效,是因为 All Windows environment variables are imported when Cygwin starts.
  3. 如果 $IDE 被定义并且具有 AndroidStudio 的值,那么它将 cd $OLDPWD.

如果您不喜欢在停靠模式下使用 Mintty,请转至 Android Studio 菜单文件 - 其他设置 - 默认设置并搜索终端。在 shell 路径中,您可以使用以下内容:

"D:\cygwin\bin\mintty.exe" -i /Cygwin-Terminal.ico D:\cygwin\bin\bash.exe -l -c "cd $OLDPWD ; exec bash"

这里有个小技巧。您必须按上述方式引用,否则您可能会收到 "java.io.IOException: Couldn't create a PTY" 错误消息。

请记住,您需要调整命令路径。

只是分享,希望对其他人有所帮助。我设法在没有创建任何额外脚本或复杂命令行的情况下做到这一点。

Settings->Tools->Terminal->Shell Path下,您可以设置:

  • $HOME 开始使用 Cygwin Bash 终端(与 Mintty 使用的相同):

    <CYGWIN_INSTALL_LOCATION>\bin\bash.exe" -l

  • 使用 Cygwin Bash 终端(与 Mintty 使用的相同)并移动到项目文件夹:

    <CYGWIN_INSTALL_LOCATION>\bin\bash.exe" -l -c "cd $OLDPWD ; exec bash"

  • 要使用Git Bash:

    <GIT_INSTALL_LOCATION>\bin\bash.exe" -l

我的回答是基于同一个问题的其他答案。我只是将它们简化了一点...