13 Nov 2024
Styling Text via Keyboard in Org and Markdown
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:
- Style text using logical names (e.g. bold, italic, code, …)
- 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.
Past Articles
21 OCT 2024 |
Announcing Casual (Redux) and ReorganizationMore reorganization for the Casual packages, hopefully for the last time. Announcing Casual, now on MELPA. read more |
8 OCT 2024 |
Referencing Org Table Cells with Text RegionsManually dealing with Org table references is not fun. This post proposes a better way using a text region to generate an Org table reference. read more |
23 SEP 2024 |
Announcing Casual EditKitAnnouncing Casual EditKit, an attempt to surface all the wonderful editing commands in Emacs. read more |
17 SEP 2024 |
Migrating MacPorts in 2024MacPorts now supports built-in migration. Finally. read more |
9 SEP 2024 |
Announcing Casual Symbol OverlayHighlighting symbols in a programming language editor is genuinely useful. For Emacs, Symbol Overlay gives you that. Casual Symbol Overlay offers a Transient menu to make using Symbol Overlay even easier. read more |
3 SEP 2024 |
Announcing Casual AgendaAnnouncing Casual Agenda read more |
27 AUG 2024 |
Updating Built-In Emacs PackagesTL;DR is to set package-install-upgrade-built-in to t to update a built-in package. But a more interesting question is what should the Emacs package ecosystem look like in the 21st century? read more |
5 AUG 2024 |
Contextual Interfaces in Casual CalcThe most recent update to Casual Calc makes it more contextual. This post describes what that means. read more |
28 JUL 2024 |
Announcing Casual BookmarksAnnouncing Casual Bookmarks, a porcelain for the Emacs bookmarks list. read more |
Page 1 / 13 >