M-@
is mark-word
, but it does not grab the part of the word that is before point.
You can write a command (or create a keyboard macro) that does M-<left>
(which is command left-word
) followed by M-@
, to pick up also the first part of the word.
For example:
(defun mark-whole-word (&optional arg allow-extend)
"Like `mark-word', but selects whole words and skips over whitespace.
If you use a negative prefix arg then select words backward.
Otherwise select them forward.
If cursor starts in the middle of word then select that whole word.
If there is whitespace between the initial cursor position and the
first word (in the selection direction), it is skipped (not selected).
If the command is repeated or the mark is active, select the next NUM
words, where NUM is the numeric prefix argument. (Negative NUM
selects backward.)"
(interactive "P\np")
(let ((num (prefix-numeric-value arg)))
(unless (eq last-command this-command)
(if (natnump num)
(skip-syntax-forward "\\s-")
(skip-syntax-backward "\\s-")))
(unless (or (eq last-command this-command)
(if (natnump num)
(looking-at "\\b")
(looking-back "\\b")))
(if (natnump num)
(left-word)
(right-word)))
(mark-word arg allow-extend)))
And if you want to point all global mark-word
key bindings (such as M-@
) to mark-whole-word
instead:
(global-set-key [remap mark-word] 'mark-whole-word)