notes from /dev/null

by Charles Choi 최민수


Styling Text via Keyboard in Org and Markdown

13 Nov 2024  Charles Choi

A recent Mastodon post by Christian Tietze asked for how one could style text in Org with a keyboard. There is an existing command org-emphasize that can do this but it has a user interface design flaw: It requires the user to know ahead of time the markup character used to style the text. For many, recalling this Org-specific character is a chore. A friendlier command design would let the user specify the style (e.g. bold, italic, code, etc.) to markup the text.

A while back, I wrote a post describing how to style text in Org and Markdown using mouse-driven menus. Thinking about Tietze’s ask, I realized I had done most of the work towards building a keyboard-driven command to style text. So I set about to followup with making that command. This post details that effort.

In thinking about keyboard-driven text styling, I wanted to achieve two things:

  1. Style text using logical names (e.g. bold, italic, code, …)
  2. Minimize the work required to select the text to be styled.
    • Infer from the point what the text region to be styled should be.

A nice feature of the Markdown styling commands is that they will try to infer from the point (cursor position) what region of text to apply the style to. No such facility that I know of exists in Org. But it’s not all gravy for Markdown. The same Markdown styling commands will only work on what Emacs defines to be a word. This makes it a hassle to deal with continuous text (that is, no spaces or linebreaks) that embeds dashes (-) or underscores (_). This is because Emacs navigation by word consider the dash and underscore characters to be word separators.

No worries though as this is Emacs: There’s always a better way.

In building Casual EditKit, I became familiar with commands that operated on balanced expressions (aka sexp). Using them, I observed that they treated continuous alphanumeric text with dashes and underscores as a single entity. With this knowledge I set out to build the command cc/emphasize-dwim.

Here’s a video of it in action.

Closing Thoughts

NGL, I’ve been pleasantly surprised at how well this has worked out, so much so that it’s motivated me to write this post. Interested readers are encouraged to try this code out.

As far as what keybinding to use for cc/emphasize-dwim, I’ve landed on using C-/ for now, which by default is bound to undo. Since my daily driver is macOS, I assign M-z for that job. As always, it’s left to the reader to choose a keybinding of their preference.

emacs   org mode

 

AboutMastodonInstagramGitHub

Feeds & TagsGet Captee for macOS

Powered by Pelican