3

For example in a org file (cursor represented as |):

* heading
    this is body |of the heading
    1. some
    2. list

After 'org-beginning-of-line' :

* heading
    |this is body of the heading
    1. some
    2. list

Is there a good way to do this without losing org-mode syntax awareness org-beginning-of-line provided ?

4
  • See: emacsredux.com/blog/2013/05/22/…
    – mankoff
    Commented Sep 27, 2014 at 14:44
  • Specifically for org-mode I don't see any value in manually indenting like you seem to do. I suggest you try using org-mode with this setting: (setq org-startup-indented t).
    – paprika
    Commented Sep 27, 2014 at 15:01
  • @mankoff, I am talking about an implementation that will work well with org-mode, not just ignore all org mode syntax.
    – Rangi Lin
    Commented Sep 28, 2014 at 3:42
  • @paprika I did not manually indent the text like that, just FYI.
    – Rangi Lin
    Commented Sep 28, 2014 at 3:44

2 Answers 2

3

A possibility would be to add an advice to org-beginning-of-line

(defadvice org-beginning-of-line (around ad-org-beginning-of-line-around act)
  "Move back to indentation first, then to beginning of line"
  (let ((initial-position (point)))
    ad-do-it
    (when (looking-at "^ +")
      (when (equal initial-position
                   (progn (back-to-indentation)
                          (point)))
        (beginning-of-line)))))

But really, the only advantage of doing that is to keep using C-a (which behaves differently on headers and items). Ideally, it would probably be better to start using the "proper" binding for back-to-indentation directly: M-m

4
  • Your advice does work, but it also make org-beginning-of-line not able to move to the first character of the line.
    – Rangi Lin
    Commented Sep 28, 2014 at 3:36
  • I already use back-to-indentation. But I use C-a in other mode with implementation like @kaushalmodi's answer. Just wondering whether I can make C-a works like I do in other mode.
    – Rangi Lin
    Commented Sep 28, 2014 at 3:39
  • @RangiLin I edited my code so that hitting C-a repeatedly would toggle between beginning of line and indentation position.
    – Sigma
    Commented Sep 28, 2014 at 4:03
  • Ah, nice, I didn't think of that :), it works pretty well. Thank you.
    – Rangi Lin
    Commented Sep 28, 2014 at 4:56
0

I have bound C-a to the back-to-indentation-or-beginning-of-line function below that I got from @lunaryorn's init.el. I recommend having a look at that init.el for more useful snippets like this.

;; Make C-a toggle between beginning of line and indentation
;; https://github.com/lunaryorn/stante-pede/blob/master/init.el
(defun back-to-indentation-or-beginning-of-line (arg)
  "Move point back to indentation of beginning of line.
Move point to the first non-whitespace character on this line.
If point is already there, move to the beginning of the line.
Effectively toggle between the first non-whitespace character and
the beginning of the line.
If ARG is not nil or 1, move forward ARG - 1 lines first.  If
point reaches the beginning or end of the buffer, stop there."
  (interactive "^p")
  (setq arg (or arg 1))

  ;; Move lines first
  (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))))

I have bound this to my minor mode key map so that it can override the C-a bindings of all other modes (including org-mode).

1
  • This implementation does not aware of org mode syntax, which is why I prefer use org-beginning-of-line in org mode.
    – Rangi Lin
    Commented Sep 28, 2014 at 3:27

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.