If the cursor is placed on a whitespace and there're multiple whitespaces between 2 words, how can I move the cursor backwards to the end of the 1st word?
You could call forward-whitespace
with a negative prefix argument. You can bind this to a key with a lambda. For example, bind it to M-B
:
(global-set-key (kbd "M-B") (lambda () (interactive) (forward-whitespace -1)))
You could
- move cursor to the beginning of the first word with
M-b
(backward-word
) - and then move forward with
M-f
(forward-word
), which place cursor after the end of the first word.
For example (*
means point):
0: first * second
1: *first second
2: first* second
If you want always go to the end of the word when moving backward, then use in init.el
(defun backward-word-end ()
"Move backward to the end of the word."
(interactive)
(backward-word 2)
(forward-word))
(global-set-key (kbd "M-b") 'backward-word-end)
P.S. But don't forget about local-set-key
and bind-key
if you use package
.
-
1A note that the
backward-word-end
function you wrote might not always work as intended.. What if the point is already at the end of a word? IMO, theM-b M-f
approach you mention above is the best bet here. – Kaushal Modi Aug 5 '16 at 13:18 -
That's how I do that now, but I don't want to anymore. I want to move it to the end of a word immediately. – Johshi Aug 5 '16 at 14:36
-
M-b M-f
? Your description is not too clear ("first word"?). Try giving a concrete example, showing where the cursor is and just where you want to move it. – Drew Aug 5 '16 at 16:39