Emacs: Get the path for current buffer from command-line

At work, I use Emacs (nowadays with the Spacemacs configuration). My favorite Git history browser is tig. When I want to see the git history or blame for the file I’m currently editing, what do I do?

There are many possible solutions, but here’s mine: a small script called cur that returns the path to the buffer currently active in Emacs.

#!/bin/sh
emacsclient -e '(buffer-file-name (window-buffer))' | \
    sed -e 's/^"//' -e 's/"$//'

It’s easy to use with tig – or any other command-line tool you might need:

tig blame $(cur)

About the author: My name is Miikka Koskinen. I'm an experienced software engineer and consultant focused on solving problems in storing data in cloud: ingesting the data, storing it efficiently, scaling the processing, and optimizing the costs.

Could you use help with that? Get in touch at miikka@jacksnipe.fi.

Want to get these articles to your inbox? Subscribe to the newsletter: