10

I know I can use *bold*, /italic/, _underline_, =verbatim=, ~code~, and +strike-through+ to apply markup to text in org-mode. But how do I create key bindings for applying these changes to a region of text? For example, if I want to make some text bold, is there an easier way than

  1. navigating to the front of the text
  2. prefixing it with a *
  3. navigating to the end of the text
  4. appending a *?

I can't seem to find any command that "bolds" a stretch of text, such as org-make-region-bold. Is there such a command?

11
  1. Mark you region
  2. Click C-c C-x C-f * for bold, C-c C-x C-f / for italic etc.

org-emphasize is an interactive compiled Lisp function in `org.el'.

(org-emphasize &optional CHAR)

Insert or change an emphasis, i.e. a font like bold or italic. If there is an active region, change that region to a new emphasis. If there is no region, just insert the marker characters and position the cursor between them. CHAR should be the marker character. If it is a space, it means to remove the emphasis of the selected region. If CHAR is not given (for example in an interactive call) it will be prompted for.

6

You can make use of electric-pair-mode1 to facilitate insertion of mark-up characters in org-mode. Add the following code to your init-file:

(electric-pair-mode 1)

(defvar org-electric-pairs '((?\* . ?\*) (?/ . ?/) (?= . ?=)
                             (?\_ . ?\_) (?~ . ?~) (?+ . ?+)) "Electric pairs for org-mode.")

(defun org-add-electric-pairs ()
  (setq-local electric-pair-pairs (append electric-pair-pairs org-electric-pairs))
  (setq-local electric-pair-text-pairs electric-pair-pairs))

(add-hook 'org-mode-hook 'org-add-electric-pairs)

With this in place, all you need to do to wrap a region with mark-up characters is press the corresponding character (*, /, =, _, ~, or +) once.

As an additional benefit, Emacs will automatically insert matching pairs of mark-up characters at point if there is no region. For example, pressing * will insert ** and leave point between the two chars so you can continue typing without having to move point.


1 This mode is built-in; there's no need to install any third-party packages to use it.

  • 1
    Thank you! Honestly, this is the cleanest and most straightforward answer. The sweet surprise is that electric-pair-mode also applies the pair to a selected region. I always though that electric pairs were for inserting pairs-only. – ephsmith May 21 '18 at 0:42
  • 1
    The problem with this approach is that * and / are commonly used in typical Org mode documents e.g. * begins a header. Similarly if you're typing a slash in longform text e.g. there is an extra slash you need to delete every time. – PlagueHammer Sep 7 '18 at 22:18
3

You can use the wrap-region package (also available on Melpa) to wrap a region with any character of your choice. This of course can be configured per major mode.

(require 'wrap-region)

(add-hook 'org-mode-hook #'wrap-region-mode)

(wrap-region-add-wrapper "=" "=" nil 'org-mode) ; select region, hit = then region -> =region= in org-mode
(wrap-region-add-wrapper "*" "*" nil 'org-mode) ; select region, hit * then region -> *region* in org-mode
(wrap-region-add-wrapper "/" "/" nil 'org-mode) ; select region, hit / then region -> /region/ in org-mode
(wrap-region-add-wrapper "_" "_" nil 'org-mode) ; select region, hit _ then region -> _region_ in org-mode
(wrap-region-add-wrapper "+" "+" nil 'org-mode) ; select region, hit + then region -> +region+ in org-mode

The expand-region package complements this package very well (also available in Melpa).

Usage

  • You select the word or phrase using the key binding you chose for er/expand-region.
  • Hit * to emphasize that region as bold (in org-mode).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.