[英]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 的回答以及他提到的问题的其他回答的启发。
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
命令的事实,但是除非我明确调用exit
或logout
,否则 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 powershell and popular terminals like windows-subsystem-for-linux , cygwin and git-bash .
如果 JetBrains 最终能够支持powershell和流行的终端,如windows-subsystem-for-linux 、 cygwin和git-bash ,那就太好了。 The only shell that currently works out of the box for me is cmd .
目前对我来说开箱即用的唯一 shell 是cmd 。
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.