问题
I want to have a custom CSH prompt when I am inside a Git repo. I want the prompt to look like this if I am not in a git repo
host_name>$
But when I am inside a Git repo must turn into something like this
host_name [GIT REPO ROOT DIR]>$
I just want to display the root of the Git repo (GIT REPO ROOT DIR), so that I know in which repo I am currently in. Instead of using 'pwd' everytime.
Do you guys have any suggesstions on achieveing this? Thanks for the help
-Anish
回答1:
Here's how I did it for tcsh. Put this script in your path someplace; call it cgb:
#!/bin/bash
git branch 2> /dev/null | grep '^*' | awk '{ print $2; }'
In your .tcshrc,
alias precmd 'set cgb=`cgb`'
set prompt='...your-prompt-string... (%$cgb) % '
precmd runs before every prompt. If you only want it to run before changing directories, use alias cwdcmd instead. I prefer precmd, since it'll run when doing a git checkout, for example.
回答2:
I am still a diehard tcsh user, but I finally gave up on raw csh and switched to tcsh for many reasons, including csh's terrible support for things like this. (And I may eventually give in and switch to bash, even. :-) ) Still, it's possible to do, as @KeithThompson noted, via aliases:
alias cd 'chdir \!:* && update_prompt'
alias update_prompt 'set prompt="...stuff here... "'
where "...stuff here..." can include using backquotes to run something, such as a script to generate the [GIT REPO ROOT DIR]
part. (Write the script in something other than csh!)
Note, while I aliased cd
to chdir ...
you can just use the command itself in the alias, which you will need with pushd
and popd
.
来源:https://stackoverflow.com/questions/9973266/customize-csh-prompt-for-git-repo