Using Markdown Mode: Switching and Toggling Markup

February 18, 2016

Markdown Mode is a major mode for editing Markdown-formatted text files in Emacs. On the surface, it provides syntax highlighting (“font lock” in Emacs parlance) to provide visual cues when editing files and it defines commands and keyboard shortcuts for inserting certain markup. For example, surrounding a phrase in double asterisks (**) in Markdown makes the phrase appear in bold when converted to HTML or another format. The corresponding key sequence in Markdown Mode for inserting bold text, as you probably know if you’ve read this far, is C-c C-s s.

The documentation for Markdown Mode is now quite lengthy, so certain features go unnoticed even by long-time users. Two of these features involve ways to switch types of markup (e.g., from bold to italics). As an example, suppose I typed **really scary** and wish I had written it instead using italics as *really scary*. There are at least two ways in Markdown Mode to quickly fix the mistake.

  1. Using the kill ring: Move the point anywhere in the bold span and press C-c C-k to execute markdown-kill-thing-at-point. This removes the entire bold span and adds the contents (“really scary” without the asterisks) to the kill ring. Then press C-c C-s e to insert an empty italic span and yank the text with C-y.

  2. Using markup toggling: Markdown Mode allows you to “toggle” certain markup, including, bold, italics, and inline code. The second method is to move the point to the bold text and repeat the insertion key sequence (C-c C-s s) to remove (toggle) the asterisks. Then select the text “really scary” (i.e., place it in the active region) and press C-c C-s e to add the italic markup.

In this case, because there are multiple words, you have to activate and set the region (the default is for insertion commands to apply to the single word at the point if there is no active region, or insert empty markup otherwise). This means the first approach is a little faster in this case, but there are cases where both approaches are useful. Both methods also apply to other forms of markup such as bold, italics, and even links.

Extra Credit: Suppose I have *really scary* in the buffer now but I wish it was _really scary_ instead. Both will render in HTML as italics, but you might have a preference for the latter in plain text. You can use the Markdown Mode cycling commands to switch between underscores and asterisks in bold and italic phrases by pressing C-c C-= or C-c C– (markdown-demote and markdown-promote).