Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the vim way to select multiple instances of current word and change them?

Anyone familiar with Sublime Text's multiple cursor feature will recognize the pattern of doing the following: press a hotkey multiple times to select multiple instances of the word under the cursor and automatically create a new cursor for each of those instances; then edit each instance simultaneously, e.g. by replacing the current word with another word or whatever you want.

The multiple cursors feature is available for vim via plugin. Before using that plugin, I want (as a new vim user), to check whether there is a more natively vim-like way to achieve the same task.

For instance, I know I could use the :s command to do a search and replace (per instructions here), but that requires me to (1) type in the word I want to replace (or use the <C-r><C-a> shortcut to do so), as opposed to simply using the current word and (2) define a range. Perhaps this is the native vim way to do it, perhaps (likely!) there's another way I don't know.

So what is the native vim way?

like image 806
Ghopper21 Avatar asked May 08 '15 18:05

Ghopper21


People also ask

How do I select multiple instances?

Select multiple instances of a word ↩ Place your cursor somewhere in or next to the word you wish to select. Press Ctrl+D (Windows or Linux) or Command+D (Mac OS X) to highlight the entire word. Press Ctrl+D (Windows or Linux) or Command+D (Mac OS X) to select the next instance of the word.

How do I select text in vim?

If you want to select the entire line in a file, press V. Now when you press k or j to go up and down, vim will select the entire line above and below your cursor. Finally, you can select text in columns by pressing ctrl+v and moving up or down the block.

How do you select multiple lines in vi?

Manipulate multiple lines of text Place your cursor anywhere on the first or last line of the text you want to manipulate. Press Shift+V to enter line mode. The words VISUAL LINE will appear at the bottom of the screen. Use navigation commands, such as the Arrow keys, to highlight multiple lines of text.


2 Answers

I use the *, gn, and the . to make changes.

  • Select current word with * (go back with N)
  • Change word with gn motion. e.g. cgnfoo<esc>
  • Repeat via . command

Note: If you have many changes then using a substitution command would probably be better.

There is a nice Vimcasts episode about the gn motion: Operating on search matches using gn.

For more help see:

:h *
:h gn
:h .
like image 54
Peter Rincker Avatar answered Sep 20 '22 10:09

Peter Rincker


You can record macros in Vim by pressing q<letter>. Macros can include the n command to search for the next instance of a word. You can also go into insert mode while recording (e.g. using the c command with a motion such as iw to replace the current word). Press q to stop recording, and then press @<letter> to replay the macro once. After that, you can use @@ to repeat the macro as many times as you like.

like image 34
Kevin Avatar answered Sep 19 '22 10:09

Kevin