简体   繁体   English

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

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

I'm running IntelliJ 2018.3 on Windows 7, as well as openSUSE Leap 15. Under Windows 7, I've configured IntelliJ to use Git Bash, ie, in Settings, under Tools -> Terminal , I'm setting Shell path to:我在 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

One of IntelliJ's new features is the ability to save and reload terminal sessions (see this link ). IntelliJ 的新功能之一是能够保存和重新加载终端会话(请参阅此链接)。

It works perfectly with openSUSE, however, on Windows, while the terminal tab names are correctly restored, I always end up with a new shell.它与 openSUSE 完美配合,但是,在 Windows 上,虽然终端选项卡名称已正确恢复,但我总是以新的 shell 结束。

Is there a way to make IntelliJ and Git Bash play well together so that I can retain the current working directory and shell history after restarting IntelliJ?有没有办法让 IntelliJ 和 Git Bash 一起玩得很好,这样我就可以在重新启动 IntelliJ 后保留当前的工作目录和 shell 历史记录?

Here's a possible workaround.这是一个可能的解决方法。 It was heavily inspired by VonC's answer , as well as other answers to the question that he mentioned .它深受VonC 的回答以及他提到问题的其他回答的启发。

~/.bashrc ~/.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

I don't like the fact that I had to wrap the cd command, however Git Bash does not execute ~/.bash_logout unless I explicitly call exit or logout ;我不喜欢我必须包装cd命令的事实,但是除非我明确调用exitlogout ,否则 Git Bash 不会执行~/.bash_logout unfortunately due to this limitation, the .bash_logout variant is inadequate for the mentioned scenario.不幸的是,由于此限制, .bash_logout变体不适用于上述场景。

The workaround above also leave small junk files inside __INTELLIJ_COMMAND_HISTFILE__ parent dir, however, I couldn't do any better.上面的解决方法还在__INTELLIJ_COMMAND_HISTFILE__父目录中留下小垃圾文件,但是,我不能做得更好。

Additionally I've opened a ticket in Jetbrain's issue tracker.此外,我在 Jetbrain 的问题跟踪器中开了一张票 There are many different shells that may benefit from official support.有许多不同的 shell 可以从官方支持中受益。 It would be great if JetBrains could eventually support and popular terminals like , and .如果 JetBrains 最终能够支持和流行的终端,如 ,那就太好了。 The only shell that currently works out of the box for me is .目前对我来说开箱即用的唯一 shell 是

You can try and setup your Git for Windows bash to remember the last used path for you, as seen in " How can I open a new terminal in the same directory of the last used one from a window manager keybind? "您可以尝试为 Windows bash 设置 Git 以记住您上次使用的路径,如“如何从窗口管理器键绑定在上次使用的终端的同一目录中打开新终端?

For instance:例如:

So instead of storing the path at every invocation of cd the last path can be saved at exit.因此,不是在每次调用 cd 时都存储路径,而是可以在退出时保存最后一个路径。

My ~/.bash_logout is very simple:我的~/.bash_logout很简单:

 echo $PWD >~/.lastdir

And somewhere in my .bashrc I placed this line:在我的.bashrc某处,我放置了这一行:

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

That does not depend on Intellij IDEA directly, but on the underlying bash setup (here the Git for Windows bash referenced and used by Intellij IDEA.这并不直接依赖于 Intellij IDEA,而是依赖于底层的 bash 设置(这里是 Intellij IDEA 引用和使用的 Git for Windows bash。

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

相关问题 如何修复 Git bash 无法在 Windows 上工作 - How can I fix Git bash not working on Windows 如何使用cmder设置Intellij idea终端路径但使用git bash.exe启动? - How to setup Intellij idea terminal path with cmder but initiated with git bash.exe? 在 Intellij 终端中设置 git - Setup git in Intellij terminal 在Bootcamp下共享驱动器上使用Git工作目录时,如何防止文件模式更改? - How can I prevent file mode changes when working with a Git working directory on a shared drive under Bootcamp? Windows中当前目录和工作目录之间的区别 - Difference between Current Directory and Working Directory in Windows 如何获取当前用户目录? - How can I get the current user directory? 在Git for windows“git bash”中,如何以Windows路径格式“打印工作目录”,可以使用cmd和Windows资源管理器吗? - In Git for windows “git bash”, how to “print working directory” in Windows path format that is usable by cmd and Windows explorer? .NET WPF 记住会话之间的窗口大小 - .NET WPF Remember window size between sessions 如何在Windows上的Git / IntelliJ中避免文件中的CR? - How can I avoid CRs in files in Git/IntelliJ on Windows? 为什么我不能更改当前工作目录? - Why can't I change the current working directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM