6

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

  • 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
9

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

  • 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 at 9:50
6

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

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

2

A solution for users of evil:

In normal state, just type: viw

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.