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

前端 未结 3 1518
無奈伤痛
無奈伤痛 2021-02-01 22:20

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

相关标签:
3条回答
  • 2021-02-01 22:39

    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.

    0 讨论(0)
  • 2021-02-01 22:52

    While waiting for other answers, I'm going to post what I'm experimenting with while waiting for vim experts to answer:

    :.,$s/<C-r><C-a>/foobar/gc
    

    to substitute (the s) from the current line (the .) to the last line ($) (with the comma denoting the line range), using the <C-r><C-a> combo to copy the current word into the command, then using gc to change with confirmation, so I can hit yes/no for each instance then quit when I've done enough.

    0 讨论(0)
  • 2021-02-01 22:57

    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 .
    
    0 讨论(0)
提交回复
热议问题