Getting Proper Titlecase in Emacs

titlecase.jpg

Introduction

More often than not, I find myself needing to type titlecase in Emacs. Yet Emacs has no inbuilt way of setting titlecase, though one can easily change case to UPPER, lower and Sentence case.

Titlecase is trickier since there are many grammatical rules guiding its proper usage. One can read about these in the Chicago Manual of Style. To get proper titlecase, I first tried a Python script, which worked quite well. I had to do this in two parts. First, create the script, and then create some Elisp to call it on a region or line. It worked, but the results were inconsistent. I found the script could not always predict the many rules for titlecase. For example:

The Emacs Package Magit: How to Get Git in Your Editor of Choice

was rendered

The Emacs Package Magit: how to Get Git in Your Editor of Choice

Good grammar requires that the first word after a colon is capitalised.

I would also get inconsistency in what constitutes the small, uncapitalised letters. Most difficult was how to render technical computing names in the titles, such as Emacs packages. Was it:

Tailoring Emacs Completions with hippie-expand

Or,

Tailoring Emacs Completions with Hippie-expand

Or,

Tailoring Emacs Completions with Hippie-Expand

Solution

Enter the very useful package: https://codeberg.org/acdw/titlecase.el

It has several functions for converting text to proper titlecase, and goes some way in keeping the text within the grammatical conventions.

The only function I needed was titlecase-dwim which I bound to M-t

(global-set-key (kbd "M-t") 'titlecase-dwim)

This command will convert any region to titlecase or fall back to another command of one's choice, which by default is titlecase-line.

In hindsight, I wonder why I suffered so long!

That's all for today folks.

(c) Raoul Comninos

You can support me on PayPal:
https://paypal.me/revrari

Return to Home


Comments

To comment, sign in with your GitHub account.