Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart home in Emacs

Tags:

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.

like image 521
pupeno Avatar asked Sep 28 '08 05:09

pupeno


1 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.

like image 200
cjm Avatar answered Sep 22 '22 13:09

cjm