Quick and easy git branch prompt for zsh (and a quick rant)

0 Comments

Update (2011/12/03): I have since changed my prompt and got a little crazy with it all and took concepts from Phil's zsh prompt and took the git stuff from Brian Carper's site. It's totally going against what I have stated below (wanting something simple), however, I really liked these concepts and here is what I came up with:

Imgur


So, I have recently moved back to using vim and I have been loving it. I have specifically been using MacVim for mostly everything. However, I have also been using vim on the command line for quick editing. Mainly, I start up MacVim for a project while I just browse the filesystem and edit files as needed from the command line at times.

What I have noticed with my increased use of the command line on OSX is that bash and BSD commands don't play nicely together. For example, the cp and mv commands in OSX (or, any BSD system by default unless you're using GNU Utils, I believe) will copy what is inside of a directory - not the actual directory - if you append a trailing slash. Example:

% cp -r /Users/someguy/whatever/ /Users/otherguy/

This doesn't do what you think it does if you're a long time Linux user. That trailing slash on the end of the from portion of that command tells cp to copy what is inside of the directory. So if /Users/someguy/whatever contains 52 files, those 52 files are going to be copied into /Users/otherguy, not the directory itself.

This is completely annoying if you're used to using tab completion to the path you want to copy or move. In bash, it always appends a / if it's a directory. Sure, you could remove that /, but that's annoying when you're typing fast and in a zone.

Enter zsh! Tab completion in zsh is much more intelligent. It will append that / if it's a directory, but if you stop there and hit enter or space, it will remove that / since it's not needed! Also, it does some fancy stuff when you tab, much like the way vim auto completes when you tab through directories. This is nice.

So, on to my point. I had a nice little color coded prompt in my bash profile that would detect if I was in a directory that was a git repo and tell me what branch I was on. This is super useful for me when I code. This was relatively simple to add thanks to this post about it. However, when I switched off to zsh, I had a little trouble getting this to work. Searching the web I found very many very involved ways of doing this that had very many lines of code in your .zshrc file. Being that I like things short and simple, I reconfigured it. While this may not be as robust as other examples on the net, it works for my purposes, which is to just tell me the branch I'm on if I'm in a git repository. I also added some custom coloring to match a (vim color scheme I use)[https://github.com/gigamo/sunburst.vim]. Here is my entire .zshrc file...

alias ls='ls -G'

# Stuff for git
parse_git_branch () {
    git branch 2> /dev/null | grep "*" | sed -e 's/* \(.*\)/ (\1)/g'
}

# sunburst.vim like colors for prompt
BLACK=$'\033[0m'
RED=$'\033[38;5;167m'
GREEN=$'\033[38;5;71m'
BLUE=$'\033[38;5;111m'
YELLOW=$'\033[38;5;228m'
ORANGE=$'\033[38;5;173m'

function precmd() {
    export PROMPT="%{$RED%}%n@%m%{$BLACK%}:%{$GREEN%}%~%{$YELLOW%}$(parse_git_branch)%{$BLACK%}%# "
}

# System dependent setup stuff goes in here
foreach f (~/.zshrc.d/*); do
    . "$f"
done

Focus mainly on the precmd function. That's pretty much all there is to it. That function gets run every time, after a command is executed. So I set my prompt in there. If the git branch command shows up, it grabs the current branch you're on and appends it to the prompt. If it isn't available, you just simply don't see it.

The last bit of code should be obvious, but I have two systems I work from. My home laptop and my work laptop. I sync things over Dropbox and Github. However, I don't want all of the same environment variables and such in my .zshrc file at home that I have at work and vice-versa. So, I store those things in a ~/.zshrc.d directory. Everything else that's shared across both systems is in .zshrc.

This works for me. I know there are other ways but this is simple. That's all I wanted. Simple but effective.

Hide gnome-terminal menubar in gnome3 on Ubuntu 11.10

3 Comments

So, lately I have been using Ubuntu 11.10 inside of Parallels on my Mac. I have really enjoyed using Gnome 3. It is a vast improvement over the Gnome 2. The reason I left Linux and started using a Mac was specifically the desktop experience. I loved Linux up until around the year 2007 and I started using a Mac then because I realized the desktop wasn't going anywhere. Gnome was stuck in the 1990's and KDE is just simply an odd experience, not very intuitive and kind of reminds me of a bubble gum pop star, but without the sex appeal.

So, fast forward to today and now I find Gnome to be completely usable. I love the Activities pane. I love how it manages virtual desktops. I love the "dock" and how it only shows up in Activities mode. I love the quick search Gnome-Do like application finder. I love the notifications and how well some apps like Empathy uses it. I'm pretty happy so far... I'm going to run it in Parallels for a while at home and at work and if I find myself happy enough, I may switch back to Ubuntu full time... although, not on the MacBook Air. It's kind of a miserable experience installing it natively on one...

Anyway, I ran into an issue with gnome-terminal that I thought I could share. I don't like having the menubar show up with each new terminal that is open. So, I turned it off in the settings. However, for some reason this didn't work. Even though I unchecked "Show menubar by default in new terminals", it kept showing up.

Well, I ran across a bug on launchpad for it and a suggestion was made to remove the appmenu packages and this would fix the issue. Well, it did for me. All I did was

sudo apt-get --purge autoremove appmenu-gtk appmenu-gtk3

Then I logged out and back into Gnome and booya, no more menubar in gnome-terminal. I hope this helps someone... or possibly myself in the future.

Your browser is unsupported and squishy.

Captains log; This site was designed for the future of science! If you can't update your browser, this site just won't function or look right. However, feel free to browse it anyway. Where's Kif?