2

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

Ben James
  • 121,135
  • 26
  • 193
  • 155
ASK87
  • 21
  • 1
  • 2
  • csh (depending on the version) doesn't provide any direct way to customize the prompt. You could define aliases for the `cd`, `pushd`, and `popd` commands that update `$prompt`. What OS are you on? On many systems, `/bin/csh` is really a symlink to `/bin/tcsh`. Are the variables `$tcsh` and `$version` set? – Keith Thompson Apr 02 '12 at 17:21

2 Answers2

5

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.

Stephen White
  • 126
  • 2
  • 4
  • 2
    According to http://stackoverflow.com/questions/1593051/how-to-programmatically-determine-the-current-checked-out-git-branch, I shouldn't be using "git branch" in scripts, since it's a porcelain command. Substitute: "git rev-parse --symbolic-full-name --abbrev-ref HEAD 2> /dev/null" for "git branch ..." above. – Stephen White Oct 30 '12 at 21:46
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.

torek
  • 448,244
  • 59
  • 642
  • 775
  • Thanks @Keith Thompson and @torek. I was earlier doing this only and it works the way I want it to. I wanted to know if there was any other way of doing it. But the sad part is `git` command can get slow for a really large repo. I am thinking of finding my repo name w/o using the `git` cmd. I am planning to write a script to parse my `pwd` and get repo name as I have a specific dir structure. – ASK87 Apr 03 '12 at 04:34