In Vim, replace all occurrences of current term under cursor

Vim

Vim Problem Overview


If you press * in Vim, the editor will search for the next occurrence of the term in the same file. It saves you from having to type out the term.

Is there a quick way to replace the term currently under the cursor with a new one? One character to issue the command, typing in the new term, then Enter.

Vim Solutions


Solution 1 - Vim

Just use * and then:

:%s//new value/

If you don't specify a pattern, the substitute command uses the last searched one. Depending on the value of gdefault in your configuration, you might need to add a /g modifier to that command.

Solution 2 - Vim

You can use:

:%s/<c-r><c-w>/new value/g

where <c-r><c-w> means to literally type CTRL-rCTRL-w to insert the word under the cursor.

Solution 3 - Vim

In addition to the other answers, you could shave off some more keystrokes by adding following snippet to your .vimrc file for doing a global search and replace.

" Search and replace word under cursor using F4
nnoremap <F4> :%s/<c-r><c-w>/<c-r><c-w>/gc<c-f>$F/i

Solution 4 - Vim

I went ahead and whipped up a plugin which lets you just enter the following:

:Dr REPLACEVALUE

https://github.com/chiedojohn/vim-dr-replace

Solution 5 - Vim

Another option is to use gn:

> Search forward for the last used search pattern, like with > n, and start Visual mode to select the match.
> If the cursor is on the match, visually selects it.
> If an operator is pending, operates on the match.
> E.g., "dgn" deletes the text of the next match.
> If Visual mode is active, extends the selection until the end of the > next match.
> Note: Unlike n the search direction does not depend on the previous search command.

So if you have FOO as last search expression, you can replace its next match with BAR typing cgnBAR<Esc>, and repeat for the following matches with ..

If you want to set the word under the cursor as search expression you can type *N (or *#) to search for the next match and come back.

For example, if your cursor is under the first FOO in this line:

<div class="FOO"><span id="FOO"><b>FOO:</b> FOO</span></div>

and you type *NcgnBAR<Esc>..., you end up with this:

<div class="BAR"><span id="BAR"><b>BAR:</b> BAR</span></div>

Solution 6 - Vim

It's a feature I also desired! But not only for the word under the cursor, also for a visual selection or a motion.

As a result (and building up on your answers), I added this to my .vimrc:

nnoremap <leader>g :set operatorfunc=SubstituteOperator<cr>g@
vnoremap <leader>g :<c-u>call SubstituteOperator(visualmode())<cr>

function! SubstituteOperator(type)
    if a:type ==# 'v'
        execute 'normal! `<v`>"my'
    elseif a:type ==# 'char'
        execute 'normal! `[v`]"my'
    else
        return
    endif

    let sub = input("substitute '".getreg("m")."' with ... : ")
    execute "%s/".getreg("m")."/".sub."/gc"
endfunction

I can now press things like \sw, \sf; or \si( in normal mode or \s in visual mode and get prompt asking if what I want to substitute my selection with.

(So far I don't know if I want the same prompt as @Lieven...)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMuchinView Question on Stackoverflow
Solution 1 - VimMatteo RivaView Answer on Stackoverflow
Solution 2 - VimskeeptView Answer on Stackoverflow
Solution 3 - VimLieven KeersmaekersView Answer on Stackoverflow
Solution 4 - VimChiedoView Answer on Stackoverflow
Solution 5 - VimetuarduView Answer on Stackoverflow
Solution 6 - VimMarc MoreauxView Answer on Stackoverflow