62

I'm looking for the emacs equivalent of vi's ^.

How can I move my cursor to the first non-whitespace character in a line?

99

The command is back-to-indentation, bound by default to M-m.

14

This is what I picked up from a previous Stack Overflow question:

(defun smart-beginning-of-line ()
  "Move point to first non-whitespace character or beginning-of-line.

Move point to the first non-whitespace character on this line.
If point was already at that position, move point to beginning of line."
  (interactive)
  (let ((oldpos (point)))
    (back-to-indentation)
    (and (= oldpos (point))
         (beginning-of-line))))
(global-set-key [home] 'smart-beginning-of-line)
(global-set-key "\C-a" 'smart-beginning-of-line)
2
  • 1
    this is not what the user asked for; ^ in vim does not do this; M-m is exactly the analog of ^ in vim and hence exactly the right answer.
    – xdavidliu
    Jun 27 '19 at 3:16
  • @xdavidliu It does answer the user's question and does so in a really nice way by providing even more.
    – UTF-8
    Jun 16 '20 at 18:06
1

You can install crux

type C-a to switch cursor between beginning of line and the first non-whitespace character

1
  • the question did not ask for toggling between first non-whitespace character and first column, it just asked for the analog of ^ in vim, which is exactly M-m.
    – xdavidliu
    Jun 27 '19 at 3:17

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.