我如何设置 IntelliJ 以记住会话之间的 Git Bash 当前工作目录?

How can I setup IntelliJ to remember Git Bash current working directory between sessions?

我 运行 Windows 7 上的 IntelliJ 2018.3,以及 openSUSE Leap 15。 在 Windows 7 下,我已将 IntelliJ 配置为使用 Git Bash,即在设置中的 Tools -> Terminal 下,我将 Shell path 设置为:

C:\Program Files (x86)\Git_2.17.1\bin\bash.exe

IntelliJ 的一项新功能是能够保存和重新加载终端会话(参见 this link)。

它与 openSUSE 完美配合,但是,在 Windows 上,虽然终端选项卡名称已正确恢复,但我总是得到一个新的 shell。

有没有办法让 IntelliJ 和 Git Bash 一起玩得很好,这样我可以在重启 IntelliJ 后保留当前工作目录和 shell 历史记录?

您可以尝试将 Windows bash 的 Git 设置为 记住 您最后使用的路径,如“ How can I open a new terminal in the same directory of the last used one from a window manager keybind?"

例如:

因此,与其在每次调用 cd 时都存储路径,不如在退出时保存最后一个路径。

My ~/.bash_logout is very simple:

echo $PWD >~/.lastdir

And somewhere in my .bashrc I placed this line:

[ -r ~/.lastdir ] && cd $(<~/.lastdir)

这不直接依赖于 Intellij IDEA,而是依赖于底层的 bash 设置(这里是 Intellij IDEA 引用和使用的 Git for Windows bash。

这是一个可能的解决方法。它深受 , as well as other answers to the question that he mentioned.

的启发

~/.bashrc

if [[ -v __INTELLIJ_COMMAND_HISTFILE__ ]]; then
    __INTELLIJ_SESSION_LASTDIR__="$(cygpath -u "${__INTELLIJ_COMMAND_HISTFILE__%history*}lastdir${__INTELLIJ_COMMAND_HISTFILE__##*history}")"

    # save path on cd
    function cd {
        builtin cd $@
        pwd > $__INTELLIJ_SESSION_LASTDIR__
    }

    # restore last saved path
    [ -r "$__INTELLIJ_SESSION_LASTDIR__" ] && cd $(<"$__INTELLIJ_SESSION_LASTDIR__")
fi

我不喜欢我必须包装 cd 命令这一事实,但是 Git Bash 不会执行 ~/.bash_logout 除非我明确调用 exitlogout;不幸的是,由于此限制,.bash_logout 变体不适用于上述场景。

上面的解决方法还在 __INTELLIJ_COMMAND_HISTFILE__ 父目录中留下了小的垃圾文件,但是,我不能做得更好。

此外,我还打开了 a ticket in Jetbrain's issue tracker. There are many different shells that may benefit from official support. It would be great if JetBrains could eventually support and popular terminals like , and . The only shell that currently works out of the box for me is