Skip to content

Instantly share code, notes, and snippets.

@mmarshall540
Last active April 6, 2025 14:24
Show Gist options
  • Save mmarshall540/58b0374295f2760c9ee5058d3225a22d to your computer and use it in GitHub Desktop.
Save mmarshall540/58b0374295f2760c9ee5058d3225a22d to your computer and use it in GitHub Desktop.
Beginning-of-line or indentation on one key
(defun my/mbol-advice (arg)
"With prefix ARG other than 1, return t.
Else, go `back-to-indentation', and if point hasn't moved, return t."
(if (eq arg 1)
(let ((start (point)))
(back-to-indentation)
(eq start (point)))
t))
(advice-add 'move-beginning-of-line :before-while 'my/mbol-advice)
@mmarshall540
Copy link
Author

mmarshall540 commented Apr 5, 2025

This is some simple code to implement my preferred behavior on the "C-a" (or "<home>") key. It goes to the first non-whitespace character on the line (just like "M-m" would do). If pressed a second time (or if point was already at that location), it moves to beginning-of-line.

There are many other ways you could do this, even packages you could install for a more fine-grained approach (e.g. MWIM).

There's also a built-in feature that applies only in org-mode buffers. You can configure that by setting the value of org-special-ctrl-a/e. (The code in this gist won't interfere with it.)

@mmarshall540
Copy link
Author

mmarshall540 commented Apr 5, 2025

Updated so that if a prefix argument other than 1 is passed, move-beginning-of-line will be immediately called using the prefix argument, since back-to-indentation doesn't use a prefix argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment