3

I'm currently running Spacemacs+evil and pressing C-RET calls evil-org-org-insert-heading-respect-content-below, which inserts a a heading below the current one.

I couldn't find a dual/mirror command that would create a heading above the current one, though. Is there one, or what's a good implementation that does this?


Note: I'm self-answering with my solution, which is the first elisp I've ever written and is very tied to Spacemacs+evil, but I would love it if others could chime in with snippets that don't depend on Spacemacs nor evil and are as such more useful to the general org-mode user. Thanks!

4 Answers 4

3

Building on kbdluv's answer, here's a simpler generic version that doesn't require evil-mode:

(defun org-insert-heading-above ()
  (interactive)
  (move-beginning-of-line nil)
  (org-insert-heading))

I've bound it to C-o (normally open-line) for org-mode files:

(add-hook 'org-mode-hook
          (lambda ()
            (define-key org-mode-map (kbd "C-o") 'org-insert-heading-above)))
2

M-x org-insert-heading (usually bound to M-ret) will insert a heading above the current heading, and with a prefix arg below the current heading.

2
  • 1
    It will insert a heading above the current heading iff you are at the beginning of the line. There might be interactions with the setting of org-M-RET-may-split-line though.
    – NickD
    Commented Jan 2, 2018 at 22:07
  • @Nick exactly. My answer actually calls org-insert-heading, but first calls evil-digit-argument-or-evil-org-beginning-of-line to go to the beginning of the line.
    – kbdluv
    Commented Jan 3, 2018 at 9:32
1

This is the solution that works for me. It is unfortunately very coupled to evil-mode (and to Spacemacs, if you consider the keybindings.)

I define a function which does the same as pressing 0,iha, then bind it to ,b.

I also bind the original evil-org-org-insert-heading-respect-content-below to ,f so that the ,b/,f pair uses the "backwards/forwards" mnemonic.

(defun my-org/insert-heading-above ()
  "Same as 0 , i h"
  (interactive)
  (evil-digit-argument-or-evil-org-beginning-of-line)
  (org-insert-heading)
  (evil-append 1))

(with-eval-after-load 'org
  (spacemacs/set-leader-keys-for-major-mode 'org-mode "f" 'evil-org-org-insert-heading-respect-content-below)
  (spacemacs/set-leader-keys-for-major-mode 'org-mode "b" 'my-org/insert-heading-above))
1
  • 1
    Another limitation of this solution is that it only works if the point is on the heading (the line starting with the ***s). If it is within a heading (like in text written inside it), it turns the current line into a heading instead of inserting a heading above the current one.
    – kbdluv
    Commented Jan 2, 2018 at 19:05
0

Thanks to @kdbluv @steve-simms . I'm using Spacemacs on macOS and utilize ⌘return to insert new heading below and ⌃⌘return to insert new heading above (respect content) in both normal mode and then switch to insert mode.

(defun my-org-insert-heading-below ()
  "Insert heading below and switch to insert mode."
  (interactive)
  (org-insert-heading-respect-content)
  (evil-insert-state 1))
(defun my-org-insert-heading-before ()
  "Insert heading above current heading and switch to insert mode."
  (interactive)
  (cond
   ((org-on-heading-p) nil)
   (t                  (org-backward-heading-same-level 0)))
  (move-beginning-of-line nil)
  (org-insert-heading)
  (evil-insert-state 1))

(with-eval-after-load 'evil-maps
 (define-key evil-normal-state-map (kbd "<s-return>") 'my-org-insert-heading-below)
 (define-key evil-insert-state-map (kbd "<s-return>") 'my-org-insert-heading-below)
 (define-key evil-normal-state-map (kbd "<C-s-return>") 'my-org-insert-heading-before)
 (define-key evil-insert-state-map (kbd "<C-s-return>") 'my-org-insert-heading-before)
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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