Teaching Emacs Who You Are

By: on February 8, 2016

To some people like me, GNU Emacs is more than just the most powerful text editing system in the world. We don’t just write prose and code in Emacs, we read and send our mail from Emacs, we browse the web using Emacs or write better versions of the Vi editor in Emacs.

Sometimes however, we even want to use Emacs as different people. Jane Doe, senior coder working 9-5 for a respected software house is not Jane Doe, loving mother of two, ordering parts for the new carport. And both Jane Does are also not Julia de Klerk, writer of popular blog posts about strange fiction hiding behind a pseudonym.

All of these are ultimately the same Jane Doe, but Emacs needs to behave differently for every one of those alter egos. Different email addresses, different email signatures, maybe a different font size for writing those popular short stories at night. And sometimes, Jane the coder needs different global minor modes switched on than Julia the novelist.

So what does Jane/home aka Jane/work aka Julia/novelist do? She’ll ready chunks of setqs and some other custom code and evaluate those blocks using eval-region. But this is tedious to maintain and sometimes there is no clear indication which code block was evaluated last. So JJJ (Jane/Jane/Julia) starts to wrap things up and create global key bindings as well as a mode line indicator.

And eventually, J3 decides to publish it as an ELPA package. Except that J3 is really just Alex and I’m not a strange fiction author, but I do enjoy the works of others and wish they used Emacs to produce them.

Enter Contextual

Contextual provides profiles support for Emacs. Switching between
contexts sets global variables and runs hooks to reflect switching
the user’s identity or the working environment.

Contextual doesn’t just wrap up support for defining and switching profiles, it provides helpers to group them together in so-called contexts, so that multiple profiles from different contexts may be active at the same time. A minor mode line indicator also shows the currently active profile in the main context, which basically reflects the idea of the alter ego outlined above. So instead of writing confusing blocks of setqs, a terse domain language can be used to group variables and custom code together into profiles and contexts, and make evaluation a breeze by assigning those contexts key bindings to access them from everywhere.

So whenever Jane wants to be Jane/private, she’d run s-c c private RET, for Jane/work she’d run s-c c work RET and to become Julia, the novelist she’d run s-c c novelist RET. Contextual takes care of setting all the variables and indicates the active profile at the bottom. Additionally, Jane can use s-c f small RET whenever she changes from her HiDPI laptop screen to an external monitor to swiftly have Emacs display smaller fonts.

Jane finally doesn’t have to eval-region code blocks, anymore. Jane know’s that’s stupid. Jane is smart. Be like Jane.

M-x package-install RET contextual RET

Share

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*