CamelCase Code Helpers in Emacs

CamelCase code in Emacs

Dealing with CamelCase, or mixed case, code can be difficult on the eyes. As you are poring over thousands of lines of code for your website or app, it can be difficult to decipher mixed case code, especially if you have a font that does not easily distinguish lower from upper case characters very well.

The Emacs text editor provides some special editing modes that make working with mixed case code easier:

These are technically “minor” modes, meaning modes that are meant to be enabled within a major mode.

The technical term for “mixed case code” is “CamelCase.” CamelCase code is not uncommon but it varies by coding style. (Always be sure to observe in advanced the preferred coding style of any project to which you are planning on contributing.) Some languages recommend CamelCase for naming functions.

See this CamelCase statement example:

thisIsCamelCaseInAction

Notice that while all of these words are jammed together, each unique word begins with a capital letter to distinguish it from the others in the statement.

Underscore CamelCase Code With Glasses Mode

In some coding styles, CamelCase code is written with underscores to help differentiate the individual words. But not always. If you’re working on a project that requires all CamelCase to be written in the former style, you cannot change how everyone else is working, but Emacs can help you out.

What Glasses Mode Does

Emacs provides a minor mode called glasses-mode, which takes your CamelCase code and puts underscores between the words. This is merely for display purposes, so you can see the underscores but the final code will have those underscores omitted. This way, you can work with underscores without disturbing or disrupting the coding style that has been officially adopted by the project.

How To Enable Glasses Mode

Like all Emacs minor modes, glasses-mode can be enabled via the meta-X (M-x) command prompt. Open a prompt with M-x and type:

glasses-mode

When glasses-mode is enabled in your buffer you will see a o^o indicator in the modeline.

To disable glasses-mode follow the exact same procedure you went through to enable it.

Subword Mode

Subword mode treats upper case letters as words. This can help you giving full word editing functionality to CamelCase statements and symbols. Remember that Emacs treats words with a whole unique set of editing commands.

How Subword Mode Works

Emacs has thousands of functions that help you perform various operations on your text. Many of these functions run as commands given to the Emacs Lisp interpreter. And many of them are specifically designed to operate on words.

With subword-mode enabled, all of the many Emacs word commands will now treat uppercase letters as words. This means that CamelCase code, interspersed as it is with capital letters, will be treated as a string of multiple words instead of as one long word.

How to Enable Subword mode

To enable subword-mode all you need to do is open your command prompt with M-x and type in:

subword-mode

When subword-mode is enabled you will see a , in the mode line to indicate that the minor mode is active.


For more information about using the Emacs text editor, make sure to check out the Emacs education channel. And if you’re wondering if Emacs is installed in your VPS server feel free to contact 24/7 live support anytime. Learn more about the differences between VPS vs dedicated.

CM
Christopher Maiorana Content Writer II

Christopher Maiorana joined the InMotion community team in 2015 and regularly dispenses tips and tricks in the Support Center, Community Q&A, and the InMotion Hosting Blog.

More Articles by Christopher

Was this article helpful? Join the conversation!

Questions about our MailChannels Deployment? We have answers and are here to help!Learn More
+