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?

Pops
  • 8,143
  • 28
  • 72
  • 94
Alexander Bird
  • 1,767
  • 4
  • 18
  • 24

3 Answers3

100

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

Sean
  • 1,654
  • 2
  • 15
  • 15
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)
George
  • 376
  • 1
  • 5
  • 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 03: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

Jerry Zhang
  • 111
  • 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 03:17