tags:

views:

704

answers:

4
+10  Q: 

Smart home in Emacs

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.

+20  A: 
(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)

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 Vule'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.

cjm
Awesome! #'beginning-of-line-text in fsfmacs goes to the first textual character in the line, which is annoyingly not the first non-blank character in comment lines.
andrew
Thanks, I've included that note in my answer.
cjm
Unfortunately it doesn't work with cua enabled, in which case shift for selection doesn't work, shift home for selecting the complete line doesn't work. Any suggestions?
Alexander Stolz
@Alexander Stolz, I never use cua-mode, but try adding `(put 'smart-beginning-of-line 'CUA 'move)` after the `defun` (i.e., right before the `global-set-key` line).
cjm
cjm: that's _exactly_ how I'd have done it, too.
offby1
+4  A: 

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)
Robert Vuković
+2  A: 

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.

fivebells
Yes, if you read the accepted answer you'll notice a discussion of some of the differences between back-to-indentation and beginning-of-line-text.
cjm
+1  A: 

Thanks for this handy function. I use it all the time now and love it. I've made just one small change: (interactive) becomes: (interactive "^")

From emacs help: If the string begins with ^' andshift-select-mode' is non-nil, Emacs first calls the function `handle-shift-select'.

Basically this makes shift-home select from the current position to the start of the line if you use shift-select-mode. It's especially useful in the minibuffer.

Gerard Thornley