2

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!

2

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)))
| improve this answer | |
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))
| improve this answer | |
  • 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 Jan 2 '18 at 19:05
1

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.

| improve this answer | |
  • 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 Jan 2 '18 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 Jan 3 '18 at 9:32
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)
)
| improve this answer | |

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.