10

I want to select current word under the cursor without using mouse.
After searching this forum, I learned how double click works.
(I noticed that it is a bit complicated and there were no direct key bindings.)

What is the best way to select current word with only keyboard?
I also don't want to use emacs function or script with a customized binding
because I want to use it universally(Android studio, Jetbrains...)

3
  • Please clarify your question. Are you asking what keybinding selects the word at point? It's unclear what you mean by not wanting to use an Emacs function (they're all functions) or customized binding.
    – Dan
    Commented Aug 23, 2017 at 13:50
  • It's funny that such kbd is absent out-of-box for 20 years :/ even MS Word has such feature (F8)
    – Dima Fomin
    Commented Oct 20, 2017 at 16:23
  • 1
    Many shortcut keys combined with Shift have a selection effect, such as M-b + M-Shift-f Commented Jul 25, 2023 at 18:57

7 Answers 7

14

I usually use C-M-SPC or, with easy-kill installed, M-w w.

1
  • seems that C-M-Spc marks a word_with_underscores whereas M-@ (which I have used so far) only marks up until the first underscore. Commented Jan 22, 2019 at 9:50
7

You can use the expand-region package to do this and more.

Let | be point and (...) indicate that ... is marked.

foo-|bar

Calling er/expand-region once:

foo-(|bar)

Calling it again:

(|foo-bar)
6

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)
4
  • Can (thing-at-point 'word) be put to good use here? :) Commented Aug 23, 2017 at 15:10
  • 1
    @Caterpillar: Not directly. But if you use Thing At Point Commands (thing-cmds.el) then you can use mark-thing for "word". It does about the same thing as the code presented here.
    – Drew
    Commented Aug 23, 2017 at 15:33
  • For _id it only marks id instead of _id, what should I do to mark complete word? @Drew
    – alper
    Commented Feb 6, 2020 at 10:23
  • Use symbol or sexp, not word, if you want to include _. Or give _ word syntax instead of symbol syntax in the current buffer.
    – Drew
    Commented Feb 6, 2020 at 16:04
4

The simplest way I've come up with to mark "around" a word is the fully built in combo of C-M-SPC C-M-b (hold down Ctrl+Meta then press Space and b), AKA mark-sexp and backward-sexp. Once your fingers are trained for the paredit (and relatives) way of C-M- all the time, it's really fast. Then you've also got the remaining niceties of multiple presses of C-M-SPC to extend the selection. From Marking Objects:

Repeated invocations extend the region to subsequent expressions, while positive or negative numeric arguments move the mark forward or backward by the specified number of expressions.

(I used to use easy-mark, as mentioned in another answer here, but I couldn't work around it always losing the top of the mark stack, which was just too annoying.)

3

A solution for users of evil:

In normal state, just type: viw

3

Place the cursor on either side of the word, then hold the shift key down and hold the alt/option key down, and then use the left or right arrow.

If the cursor is not on either side of the word, then hold the alt/option key down and use the left or right arrow key to move to either side of the word before selecting it as set forth in the preceding paragraph.

1
  • if you hate arrow keys, you might use M-S-f and M-S-b (forward or backward) to select the word.
    – archer
    Commented Apr 15, 2022 at 15:04
0

How about:

(defun mark-whole-word ()
  "Like mark-word, but the whole word from the beginning"
  (interactive)
  (backward-word)
  (mark-word))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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