0

Here, two headings are collapsed, but they do have some content represented by org's s...

* Heading 1...
* Heading 2...

How can I insert a heading right below Heading 2, including below all its content? My cursor is at caret when I do M-RET: Heading 2^.... As example:

* Heading 1...
* Heading 2...
* Heading 3...

Right now, it seems the behaviour is kind of buggy, where pressing M-RET on the previous heading's ... will lead move all the content below the heading? Unfortunately this isn't the behaviour that I want..

* Heading 1...
* Heading 2
* ...
  • Go to after the ellipsis with C-n or <right>...<right> (as many as required to move you to after the ellipsis - you can use C-f instead of <right> if you prefer) - and then press M-RET. – NickD Oct 26 at 17:26
  • @NickD Hmm, this inserts a second level heading: ** instead of what I would like: * . Any way to change to same level heading behaviour? – herophant Oct 26 at 19:24
  • So you have a second level heading under Heading 2? What I do is insert a heading and then adjust the level with M-<left> or M-<right>, although there are probably slicker methods: my trouble is that the only way I can remember these things is if they are baked into my muscle memory and that's M-RET and M-<left> - anything else might as well not exist :-) But try C-u M-RET. – NickD Oct 26 at 19:32
1

You are probably looking for C-u M-RET which calls org-insert-heading with a prefix argument. The doc string for org-insert-heading says:

With a ‘C-u’ prefix, set ‘org-insert-heading-respect-content’ to a non-nil value for the duration of the command. This forces the insertion of a heading after the current subtree, independently on the location of point.

So if you are in the first-level subtree, it will go to the end of the subtree and insert a first-level heading, but if you are in a second-level subtree, it will go to the end of that and insert a second-level heading. In this second case, there is no way to guess that you want a first level heading, so you'll have to adjust the level with M-<left> or M-<right> as I mentioned in the comment.

If you really want a first-level heading at the end of the current subtree, you can define a function and bind it to a key (but IMO, that's a waste):

(defun ndk/org-insert-top-level-heading-at-end-of-current-subtree ()
   (interactive)
   (org-insert-heading '(4) nil t))

(define-key 'org-mode-map (kbd "S-<f12>") #'ndk/org-insert-top-level-heading-at-end-of-current-subtree)

The '(4) makes org-insert-heading behave the same way that it would if it were called interactively with a C-u prefix and the t forces it to insert a first-level headline instead of what it normally would. I bound it to S-<f12> because that is likely unused but you can suit yourself.

| improve this answer | |
  • No ya, C-u M-RET works fine. I just did an example for first level headings but this default behaviour is exactly what I needed. Thanks! I saw using C-h k M-RET that this keybinding should do org-insert-heading in the correct conditions, but didn't really understand the lisp code. Now it seems that doing RET on the link to org-insert-heading does mention this lol.. Does your knowledge about org-mode come from reading the manual from start to end, reading the source code or doing such experiments? (my guess is experiments, but..) – herophant Oct 27 at 1:21
  • All three - but my first encounter with Org mode was ca 2006, so I've had plenty of time to do them :-) – NickD Oct 27 at 1:43
  • Just a curiously : why not type star with a single stroke instead of C-u M-Return:-) – Vaibhav Oct 29 at 2:56
  • Note where point is: at the end of the headline but before the ellipsis that rrepresents the folded content. If you type * there, it would break the current headline. – NickD Oct 29 at 13:21

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.