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?

  • Very closely related: emacs.stackexchange.com/q/4271/115 Aug 5 '16 at 14:48
  • 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

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

  1. move cursor to the beginning of the first word with M-b (backward-word)
  2. 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."
  (backward-word 2)

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

  • 1
    A 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, the M-b M-f approach you mention above is the best bet here. 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
  • I don't want to overwrite the default behaviour of M-b.
    – Johshi
    Aug 5 '16 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.