2

I just started with Org-mode today and noticed whenever I press C-a on a heading, the cursor is moved all the way to the beginning of the line instead of the first character in the heading. Like so :-

* This i|s a heading.

after pressing C-a, the cursor moves like so :-

|* This is a heading.

I want C-a to move the cursor like so :-

* |This is a heading.

This is pretty minor quirk, but I've bound C-a to the below function (I don't remember where I got this from) and I'd want the behvaior of it to be consistent across all emacs modes.

(defun smarter-move-beginning-of-line (arg)
  (interactive "^p")
  (setq arg (or arg 1))

  (when (/= arg 1)
    (let ((line-move-visual nil))
      (forward-line (1- arg))))

  (let ((orig-point (point)))
    (back-to-indentation)
    (when (= orig-point (point))
      (move-beginning-of-line 1))))

The problem with Org-mode heading is, we use asterisks to indicate heading levels ( * -> level 1, ** -> level 2, and so on,) and they HAVE to be entered as the first character on the line and all these commands which move to the beginning of the line think the indentation level is the first character of the line. The solution should be pretty simple, but sadly I haven't learned elisp as of yet. All we have to do is :-

  • identify if the line is a heading or not.
    • if so, ignore all the asterisks and move forward by one character
    • if not, invoke the smart-beginning-of-line command I mentioned in the post.
  • I don't use Org mode, but I'd be surprised if there isn't already a command (and even a key bound to it) that does what you're looking for. – Drew Aug 14 '16 at 20:57
  • there is a command org-beginning-of-line. But it does the same as C-a. – Chakravarthy Raghunandan Aug 14 '16 at 21:02
4

This is supported right out-of-the-box, by setting:

(setq org-special-ctrl-a/e t)

As a bonus C-e handles tags similarly to C-a handling asterisks.

1

This is a slight modification of commands to-indentation-repeat-backward and to-indentation-repeat-forward, from library misc-cmds.el.

You can repeat these commands, to continue moving the same way on the previous (if backward) or next (if forward) line.

(defun to-org-indentation-repeat-backward ()
  "Move to the first non-whitespace, non-* char on this line, or eol if none.
If already there then do the same on the previous line."
  (interactive "^")
  (let ((opt  (point)))
    (back-to-indentation)
    ;;(while (eq ?* (char-after))
    (when (looking-at "[*]+.") (goto-char (match-end 0)))
    (when (= opt (point))
      (forward-line -1)
      (back-to-indentation)
      (when (looking-at "[*]+.") (goto-char (match-end 0))))))

(defun to-org-indentation-repeat-forward ()
  "Move to the first non-whitespace, non-* char on this line, or eol if none.
If already there do the same on the next line."
  (interactive "^")
  (let ((opt  (point)))
    (back-to-indentation)
    (when (looking-at "[*]+.") (goto-char (match-end 0)))
    (when (= opt (point))
      (forward-line 1)
      (back-to-indentation)
      (when (looking-at "[*]+.") (goto-char (match-end 0))))))
  • Does not work. Edited the post stating the problem. Please take a look – Chakravarthy Raghunandan Aug 14 '16 at 21:32
  • Updated, to skip the * chars in a heading. – Drew Aug 14 '16 at 22:00
  • Works perfectly. Thank you for your time :) I've one final question. I've already bound C-a to smart-beginning-of-line. Is it possible for me to bound C-a to your answer ONLY only org-mode and every where else, I'd like to use smart-beginning-of-line. – Chakravarthy Raghunandan Aug 14 '16 at 22:14
  • Does to-indentation-repeat-backward (same thing without the (when (looking-at...)...) lines) do what you want outside Org mode? If so, just stick (when (derived-mode-p 'org-mode)...) around the (when (looking-at...)...) lines. If not, define a command that calls one or the other (your smart... or to-org...), depending on (derived-mode-p 'org-mode). – Drew Aug 14 '16 at 22:47
  • Or - better - just bind the org-specific command in the Org keymap. That's the usual way to get mode-specific behavior. – Drew Aug 14 '16 at 22:52

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.