60

I'm looking for a Vim command to select a word under cursor in normal mode, like double-clicking by mouse. Does it exist like this?

Keita
  • 1,118
  • 2
  • 11
  • 17
  • @pfnuesel `*` selects the next occurrence of the word under the cursor, because it's a search command, not a select command. – Dan Lowe Jan 30 '16 at 23:02
  • 1
    @DanLowe Yep, you're right, i deleted my comment. But `viw` should do the job. – pfnuesel Jan 30 '16 at 23:03
  • Won't that put you in visual mode? Is it even possible to select stuff in normal mode? – MinusFour Jan 30 '16 at 23:06
  • 1
    Thanks @pfnuesel and @Matthew-strawbridge for answering my question. The command `viw` is what I was looking for :) – Keita Feb 01 '16 at 18:06
  • I mapped `v` to `viw` in normal mode to select a word with one-key typing. It seems it's working well. – Keita Feb 01 '16 at 18:08
  • The Vim documentation `:help visual-operators` explains how to select a word `vaw` and an inner word `viw`, as well as paragraphs and various variations of parenthesis blocks and quoted strings. – Paul Rougieux Oct 13 '20 at 08:28
  • https://stackoverflow.com/a/66910215/9384511 – ABN Apr 01 '21 at 19:30

5 Answers5

95

You can use * and/or # to search for the word under the cursor or viw to visually select the word under the cursor.

pfnuesel
  • 10,855
  • 11
  • 51
  • 63
  • 4
    I know `v` and `w`, but what does `i` do while in visual mode? – minitauros Dec 19 '18 at 10:50
  • 11
    @minitauros `i` stands for inner or inside here. `viw` visually selects the inner word. It will be immediately clear what's meant if you open a document with parentheses. Then put your cursor anywhere inside the parentheses and type `vi(`. It will select everything inside the parentheses. This also works with quotation marks, e.g. `vi"` or `vi'`. If you want to select the parentheses (or quotation marks) as well, you can use `va(` (or `va"`). But I do not know, what the `a` here stands for. And, of course, you cannot just visually select, but also e.g. delete (`di(`) or yank (`yi(`). – pfnuesel Jan 02 '19 at 14:57
  • 2
    `a` apparently means "around". – pfnuesel Sep 17 '19 at 11:06
  • 1
    @pfnuesel If your cursor is in the middle of the word, `vw` selects from the middle to the end of the word, while `viw` selects the whole word even if your cursor is in the middle of the word. – stackzebra Feb 11 '20 at 11:49
  • 3
    And `ciw` to clear the word under the cursor and swap into insert mode so you can start typing something to replace it. – MattCochrane Jun 08 '20 at 10:25
42

viw does a visual select inside the word. Similarly yiw copies (yanks) the word.

Matthew Strawbridge
  • 18,016
  • 10
  • 65
  • 86
21

Personally, I am prefering vaw, yaw instead of viw or yiw as a indicates around.

On the similar lines. I use

vat to select tag. va( or va{ v% to select matching closing tag. In some other places ev%

It is making more sense to me as the intention is to select the complete word not inside.

At the end, it all comes down to our personal preference.

0

To select a word under a cursor I use the combination of bve

Be aware though that if the cursor points to a first character you just need ve combination.

Fupslot
  • 35
  • 5
0

After using *, if you want to use the text in a command you can do the <C-r> <C-w> trick too (after pressing : or your equivalent).

okay56k
  • 5,856
  • 7
  • 35
  • 86