Smart home in Emacs

Viewed 3535

Can you have smart behavior for the home key in Emacs? By smart I mean that instead of going to the character number 0, it should go to the first non-blank character, and go to 0 on a second pressing, and back to the first non-blank in a third and so on. Having smart end would be nice as well.

7 Answers
(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 "^") ; Use (interactive) in Emacs 22 or older
  (let ((oldpos (point)))
    (back-to-indentation)
    (and (= oldpos (point))
         (beginning-of-line))))

(global-set-key [home] 'smart-beginning-of-line)

I'm not quite sure what smart end would do. Do you normally have a lot of trailing whitespace?

Note: The major difference between this function and Robert Vuković's is that his always moves to the first non-blank character on the first keypress, even if the cursor was already there. Mine would move to column 0 in that case.

Also, he used (beginning-of-line-text) where I used (back-to-indentation). Those are very similar, but there are some differences between them. (back-to-indentation) always moves to the first non-whitespace character on a line. (beginning-of-line-text) sometimes moves past non-whitespace characters that it considers insignificant. For instance, on a comment-only line, it moves to the first character of the comment's text, not the comment marker. But either function could be used in either of our answers, depending on which behavior you prefer.

This works with GNU Emacs, I didn't tried it with XEmacs.


(defun My-smart-home () "Odd home to beginning of line, even home to beginning of text/code."
    (interactive)
    (if (and (eq last-command 'My-smart-home)
            (/= (line-beginning-position) (point)))
    (beginning-of-line)
    (beginning-of-line-text))
)

(global-set-key [home] 'My-smart-home)

Note that there is already a back-to-indentation function which does what you want the first smart-home function to do, i.e. go to the first non-whitespace character on the line. It is bound by default to M-m.

My version: move to begining of visual line, first non-whitespace, or beginning of line.

(defun smart-beginning-of-line ()
    "Move point to beginning-of-line or first non-whitespace character"
  (interactive "^")
  (let ((p (point)))
    (beginning-of-visual-line)
    (if (= p (point)) (back-to-indentation))
    (if (= p (point)) (beginning-of-line))))
(global-set-key [home] 'smart-beginning-of-line)
(global-set-key "\C-a" 'smart-beginning-of-line)

The [home] and "\C-a" (control+a) keys:

  • Move the cursor (point) the the beginning of the visual line.
  • If it is already at the beginnng of the visual line, then move it to first non-whitespace character of the line.
  • If it is already there, then move it to the beginning of the line.
  • While moving, keep the region (interactive "^").

This is taken from @cjm and @thomas; then I add the visual line stuff. (Sorry for my broken English).

Related