March 31, 2009 at 09:23 PM | categories: mercurial, bash | View Comments

In honor of the decision to move Python development to Mercurial, I decided to write something helpful. I've found __git_ps1() to be incredibly useful, so, here's my take on _hg_ps1():

    local basedir=$(pwd)

    while [ '/' != "${basedir}" ]; do
        if [ -d "${basedir}/.hg" ]; then

        basedir="$(readlink -f "${basedir}/..")"

    [ '/' == "${basedir}" ] || echo -n "${basedir}"

    local g=$(_hg_root)
    if [ -n "${g}" ]; then
        local branch

        branch=$(hg branch)
        if [ -f "${g}/.hg/bookmarks.current" ]; then
            branch="$(< "${g}/.hg/bookmarks.current"):${branch}"

        if [ -n "${1-}" ]; then
            printf "$1" "${branch}"
            printf " (%s)" "${branch}"

Yes, I know that the hg root command does the same thing as my _hg_root(), but it felt slow running mercurial before printing each prompt. The only thing that's missing is showing when merges are in progress. I'll try to add that the next time I need to do a manual merge.

Update: It seems that some of my repositories don't have a .hg/branch file so I'm calling hg branch for that info. Also, the readlink line in _hg_root() did not properly handle spaces.

Saving ViM Macros

March 11, 2009 at 09:32 AM | categories: tips, vim | View Comments

I use ViM macros (aka complex-repeat) all the time. Occasionally, I find that it would be nice to reuse one of my macros across editing sessions. Turns out it's pretty easy to save the macro. Just add a line like the following to a file that gets sourced when your file is opened by ViM (This example is from ~/.vim/ftplugin/rst.vim).

let @h = "yypVr"

Now, whenever I open a ReStructured Text file, I can hit @h and my macro will run.

Sparse Files and tar

March 08, 2009 at 06:50 PM | categories: tips | View Comments

GNU tar provides the -S option to efficiently handle sparse files. Of course it only works if you create the tarball with the -S option. That is all.

More Useful ViM Tags

March 04, 2009 at 08:35 PM | categories: tips, vim | View Comments

Frequently, I find myself writing C code that requires the use of struct ifreq. There are plenty of fields there and of course there are other structures that I never remember. I could always look in the headers, but it ends up being a good amount of digging before I find the real definition and all its accompanying pieces. So, I decided to let ctags and ViM do the work for me. I created ~/bin/update_local_tags with the following contents:

[ -d ~/.localtags ] || mkdir ~/.localtags
ctags -f ~/.localtags/usrinclude.ctags --exclude=vector\*.hpp -R /usr/include >/dev/null 2>&1

Then I created ~/.vim/ftplugin/c.vim with the following contents:

setlocal tags+=$HOME/.localtags/usrinclude.ctags

For good measure I copied c.vim to cpp.vim so it would be loaded for C++ file types also.

Now I just have to run update_local_tags to generate a tag file for everything in /usr/include and ViM automatically includes that list whenever I edit a C or C++ file. Updating the tags file is still a manual process, I should probably attempt to hook it into apt. Observant readers will have noticed that I exclude vector*.hpp when generating the tags. Boost includes a few generated header files that match this pattern and swell the tag file to almost 750 MiB.