2

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?

Johshi
  • 131
  • 4
  • Very closely related: http://emacs.stackexchange.com/q/4271/115 – Kaushal Modi Aug 05 '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 05 '16 at 16:39

2 Answers2

2

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

Konstantin Morenko
  • 1,407
  • 9
  • 19
  • 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. – Kaushal Modi Aug 05 '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 05 '16 at 14:36
  • I don't want to overwrite the default behaviour of M-b. – Johshi Aug 05 '16 at 14:37
2

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)))
Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
glucas
  • 20,175
  • 1
  • 51
  • 83