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

Drew
  • 75,699
  • 9
  • 109
  • 225
Peter Hwang
  • 211
  • 1
  • 2
  • 6
  • 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 Aug 23 '17 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 Oct 20 '17 at 16:23
  • Many shortcut keys combined with Shift have a selection effect, such as `M-b` + `M-Shift-f` – Time Killer Jul 25 '23 at 18:57

6 Answers6

14

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

Manuel Uberti
  • 3,150
  • 18
  • 36
  • 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. – Adam.at.Epsilon Jan 22 '19 at 09: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)
Tianxiang Xiong
  • 3,848
  • 16
  • 27
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)
Drew
  • 75,699
  • 9
  • 109
  • 225
  • Can `(thing-at-point 'word)` be put to good use here? :) – Caterpillar Aug 23 '17 at 15:10
  • 1
    @Caterpillar: Not directly. But if you use [Thing At Point Commands](https://www.emacswiki.org/emacs/ThingAtPointCommands) ([`thing-cmds.el`](https://www.emacswiki.org/emacs/download/thing-cmds.el)) then you can use `mark-thing` for `"word"`. It does about the same thing as the code presented here. – Drew Aug 23 '17 at 15:33
  • For `_id` it only marks `id` instead of `_id`, what should I do to mark complete word? @Drew – alper Feb 06 '20 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 Feb 06 '20 at 16:04
3

A solution for users of evil:

In normal state, just type: viw

izkon
  • 1,798
  • 10
  • 23
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.

lawlist
  • 18,826
  • 5
  • 37
  • 118
  • if you hate arrow keys, you might use M-S-f and M-S-b (forward or backward) to select the word. – archer Apr 15 '22 at 15:04
2

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

Micah Elliott
  • 221
  • 3
  • 6