66

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,393
  • 29
  • 76
  • 95
Alexander Bird
  • 1,827
  • 4
  • 19
  • 25
  • 1
    https://emacs.stackexchange.com/questions/48121/is-there-an-emacs-command-to-go-to-end-of-code-line-before-line-comment-and-whi/48160#48160 – rofrol Jul 29 '22 at 11:31

3 Answers3

105

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

Sean
  • 1,734
  • 2
  • 15
  • 15
15

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
  • 396
  • 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
  • 1
    @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
  • While not exactly what was asked, it gave me the answer I was looking for. Nice to have this answer in a related question IMO. Saved me more searching time. – squeegee Aug 13 '22 at 23:36
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