Emacs: move point to last character without spaces
M-m( back-to-indentation
) moves point to the first non-whitespace character in the line. I would like to do the opposite: move point to the last character with no spaces in the line. I haven't been able to find a "built-in" command for this, and I don't know enough about ELisp to write anything, so any help would be appreciated please.
source to share
Usually in this situation I want to go to the last character with no spaces, and also remove the trailing space, so I use this:
M-\ runs the command delete-horizontal-space, which is an interactive
compiled Lisp function in `simple.el'.
On the rare occasion that I would like to preserve whitespace, I just use M-b M-f( backward-word
, forward-word
), which is usually close enough.
source to share
I wrote this function to bind to Ce (usually move-end-of-line
). Ce works as usual, but if your pointer is already at the end of the line, it will remove trailing spaces.
(defun my/smarter-move-end-of-line (arg) "Move to the last non-whitespace character in the current line. Move point to end of this line. If point is already there, delete trailing whitespace from line, effectively moving pointer to last non-whitespace character while also removing trailing whitespace. If ARG is not nil or 1, move forward ARG - 1 lines first." (interactive "^p") (setq arg (or arg 1)) ;; Move lines first (when (/= arg 1) (let ((line-move-visual nil)) (forward-line (1- arg)))) (let ((orig-point (point))) (move-end-of-line 1) (when (= orig-point (point)) (delete-horizontal-space))))
Remap Ce:
;; remap C-e to 'smarter-move-end-of-line' (global-set-key [remap move-end-of-line] 'my/smarter-move-end-of-line)
source to share
I think phils answered your question. Another prisoner of war. Leading white spaces are very annoying, invisible and error-prone (?). So I have a hook before-save-hook
to remove them.
;;; delete nasty hidden white spaces at the end of lines
(add-hook 'before-save-hook 'delete-trailing-whitespace)
So your indented operation becomes simple for me C-e
.
source to share