Lately I’ve been using Github’s “Gist” service to post code snippets to this blog, so it would be really handy if I could quickly post new snippets from Emacs.

There is a project by Chris Wanstrath called gist.el, but it’s a little out of date. I went looking for more recent fork of the project, and found a promising one by Masahiro Hayashi.

This project apparently depends on gh.el, a library of Emacs GitHub functions by the same author. This library is available from the Marmalade archive, so I install interactively it using the Emacs 24 packaging system.

I note in fiddling around that gh.el seems to croak if the “eieio” library isn’t already loaded, so I add that to my init.el:

The version of gist.el in Marmalade is not the latest and working-est, so I’ll need to use the version from GitHub instead. I’ll use git submodules to include the external repo inside my Emacs config repo:

Now I need to arrange for that directory to be added to my load-path. I know I’ll be adding more external repos there in the future, so I write some generic code to add all the directories in elisp/external to the load path.

I evaluate this code by placing the cursor after it and hitting C-x C-e, and then add a require for the gist.el package. I also configure it to use HTTP basic auth.

Using that auth mode it’s going to prompt me for my password all the time. I need to set the gist-user-password variable to avoid this, but I don’t want to put the variable in init.el where it will be committed to my public repo. Instead, I add some lines which will load a secrets.el file if it exists:

I set my GitHub password in that file:

Then I chmod secrets.el to be accessible only by me, and use Magit to add it to .gitignore so it won’t be committed.

I don’t want to be typing “gist-region-or-buffer” all the time, so I bind it to the F8 key:

Finally, I notice that gist-region-or-buffer is getting cofused when there is no region (selection). After doing a little debugging, I use M-x customize-option to set the “mark-even-if-inactive” option to nil, and this seems to fix the issue. I’m a little worried that setting this option will have unwanted side-effects, but I’ll wait and see.

All this done, I now have a fully-functional and very convenient gist-posting function bound to F8, along with some new infrastructure for future additions.

Here’s a quick video showing off how gist.el can not only post gists, but also list and edit them:

httpv://youtube.com/watch?v=Hx4HM_39m2Q

Published by Avdi Grimm

5 Comments

  1. Cool stuff! 

    BTW, good to see a Ruby Guru using Linux (not Mac)!

    Reply
  2. can’t you just use an ssh key to get to your gists without having to have a file with your password? 

    Reply
    • Initially publishing a Gist requires that you use the GitHub API. Subsequent pushes to the Gist repo are ordinary Git operations, and can be authenticated with your SSH key like any other Git push. But the creation of the Gist (and listing Gists) must be done through the HTTP API.

      Reply
  3. Great article man, loving this series!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *