执行命令时运行 gitprompt
在 bash 中,我使用 gitprompt 显示 git 存储库的当前状态,因此我的 PS0 如下所示:[时间][存储库名称][当前路径][添加/修改的文件数]$ 这是在 ~/.bashrc 中设置的,我在其中导出 PROMPT_COMMAND
export PROMPT_COMMAND=$PROMPT_COMMAND';export PS1=$(gitprompt.pl c=\+ u=\~ f=\* A=\^ B=\\\\ F=\ \>\> statuscount=1)'
我也在使用屏幕(这可能会在这个问题中发挥作用)。当我创建一个新的屏幕窗口时,它会自动获取 .bashrc,因此我的 PS0 会使用该存储库的最新 git 状态进行更新,不幸的是,这是我的 PS0 唯一一次使用 git 状态进行更新。
我该如何设置,以便每次在终端中运行命令或仅在某些命令上运行命令时我的 git 状态都会更新?
我只需要在关闭文件、移动、删除或复制时运行 git status;因此,如果我可以将提示设置为仅在这些命令上运行,那将是最好的,但我会满足于在每个 bash 命令上运行它(而不将其绑定到我的返回键)。
这是我在 ~/.bashrc 中的整个提示
export PS0='\[\e[0;32m\][\t]\[\e[1m\][\h]\[\e[0;1m\][\w]\[\e[30;1m\]%{\[\e[1;32m\][%b]\[\e[0m\][%c%u%f%t]%}%{[%B%A%F]%}\[\e[0m\]\u\$ '
PS1=$(gitprompt.pl c=\+ u=\~ f=\* A=\^ B=\\\\ F=\ \>\> statuscount=1)
export PROMPT_COMMAND=$PROMPT_COMMAND';export $PS1'
In bash, I'm using a gitprompt to show the current status of my git repository, so my PS0 looks like this: [time][repo name][current path][number of added/modified files]$
This is set in ~/.bashrc where I'm exporting the PROMPT_COMMAND
export PROMPT_COMMAND=$PROMPT_COMMAND';export PS1=$(gitprompt.pl c=\+ u=\~ f=\* A=\^ B=\\\\ F=\ \>\> statuscount=1)'
I'm also using screen (which may come into play in this question). When I create a new screen window it automatically sources the .bashrc so my PS0 updates with the latest git status for that repo, unfortunately, that is the only time my PS0 updates with the git status.
How would I set this up so my git status updates every time I run a command in terminal or only on certain commands?
I would only need to run a git status when closing a file, moving, deleting or copying; so if I could set the prompt to run only on those commands, that would be best, but I would settle for it running on every bash command (without binding it to my return key).
Here is my entire prompt in ~/.bashrc
export PS0='\[\e[0;32m\][\t]\[\e[1m\][\h]\[\e[0;1m\][\w]\[\e[30;1m\]%{\[\e[1;32m\][%b]\[\e[0m\][%c%u%f%t]%}%{[%B%A%F]%}\[\e[0m\]\u\$ '
PS1=$(gitprompt.pl c=\+ u=\~ f=\* A=\^ B=\\\\ F=\ \>\> statuscount=1)
export PROMPT_COMMAND=$PROMPT_COMMAND';export $PS1'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,您遇到的问题是您已经在单引号内,因此它不想再次解析该命令。应该可以通过将 PS1 赋值放入 bash 函数中然后设置 PROMPT_COMMAND 来解决这个问题:
也可以将 gitprompt.pl 脚本放入函数中并返回您的结果需要,这样它仅在 git 存储库内打印结果。
这可以通过类似于以下内容来实现:(
注意:这未经测试,可能需要一些调整)
It looks to me like the problem you're encountering is that you're already inside the single quotes, so it doesn't want to parse the command a second time. It should be possible to get around that by putting the the PS1 assignment inside a bash function and then setting PROMPT_COMMAND:
It may also be possible to put your
gitprompt.pl
script inside a function and return the results you need, so that it's only printing a result if it's inside a git repository.This could be achieved with something similar to the following:
(Note: This is untested, it may require a few tweaks)
您想要这样的东西:
即:通过转义命令扩展来防止立即评估 gitprompt.pl。
这样,只要需要显示 PS1,您的
$(gitprompt.pl)
就会被评估。You want something like this instead:
that is: prevent the immediate evaluation of gitprompt.pl by escaping the command expansion.
This way your
$(gitprompt.pl)
will be evaluated whenever PS1 needs to be shown.