Be Case Sensitive With Case Conversion in Emacs

There are case conversion commands built into Emacs that convert the case (upper case or lower case) for individual words, words in a series (including words searched or selected by regular expressions), or complete highlighted regions of text (like paragraphs, or multiple paragraphs in a series, or even multiple paragraphs selected by regular expressions). Case conversion commands can help you save extra typing and allow more flexibility in writing your own custom Emacs functions.

Case Conversion on Words

All of the commands for making individual words upper or lower case follow successively to the next word. This means you can keep running the command over each word in your sentence, as needed, from the start of the sentence to the end.

Take note that all of these commands operate on the following word after point. This means if your cursor is at the end of word, the next word will be placed in upper or lower case.

Convert Word to Lower Case with M-l

M-l runs the command downcase-word, which converts an entire word to lower case.

Upper case letters at the beginning of a word will be converted to lower case:

Escape > escape

Fully capitalized words are also converted to lower case:

ESCAPE > escape

Upper case letters at any point in the word are converted:

esCape > escape

Convert Word to Upper Case

M-u runs the command upcase-word, which converts all letters to upper case.

escape > ESCAPE

Capitalize the Following Word

In many instances, you may want to capitalize only the first letter in a string of text. For these situations you can use M-c to run the command capitalize-word.

escape > Escape

Notice that capitalize-word will only place the first letter in the word into upper case.

Case Conversion on Regions

Running case conversion on a region of text is an easy way to apply your desired conversion on a larger string of text at one time.

Note that the following region commands have been disabled in Emacs, but as you run them you will be prompted with a message explaining why, and you can select to proceed and enable the commands, or try them out once and keep them disabled.

Convert a Region to Lower Case

With C-x C-l you can run the command downcase-region, which will convert the entire highlighted region to lower case.

  1. Highlight a region of text with the mark function (C-SPC)
  2. Press C-x C-l

Notice that all upper case letters are converted into lower case.

Convert a Region to Upper Case

Conversely, you can upper case an entire region using C-x C-u, which will run the upcase-region command.

  1. Highlight a region of text with the mark function (C-SPC)
  2. Press C-x C-u

Notice as each word is converted into upper case. Note that this command does not capitalize the first letters of words but rather places every letter in upper case.


These helpful commands can be used as one-off functions to be performed as needed or built into your own custom functions. For more information about Emacs be sure to check out the Emacs education channel. To see if Emacs is installed in your VPS account feel free to contact 24/7 live support any time.

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!