Skip to main content
112 votes
Accepted

How to find and replace in Vim without having to type the original word?

I actually have a pretty similar workflow to yours, (copying and pasting blocks that are similar, then using :s to change variable names) especially when I'm writing lots of lines that are similar ...
DJMcMayhem's user avatar
  • 17.8k
26 votes

How to replace only within visual selection?

There’s \%V for that, see :h %V: %V Match inside the Visual area. When Visual mode has already been stopped match in the area that gv would reselect. This is a /zero-width match. To make sure the ...
dessert's user avatar
  • 385
26 votes

How to find and replace in Vim without having to type the original word?

The c_CTRL-R family of maps can improve your workflow quite a bit: You need not type variable names when using :s/, just use CTRL-R CTRL-W to insert the word under the cursor into the command line (...
Mass's user avatar
  • 14.2k
24 votes

How to replace each match with incrementing counter?

:let @a=1 | %s/search/\='replace'.(@a+setreg('a',@a+1))/g But beware, it will overwrite your register a. I think it is a little bit more straight forward than luc's answer, but maybe his is faster. ...
B.G.'s user avatar
  • 1,186
22 votes
Accepted

How can I repeat last substitution and with the same flags

This is exactly what :&& does: *:&&* [&] Must be the first one: Keep the flags from the previous substitute command. Examples: > :&&...
Naumann's user avatar
  • 2,769
20 votes
Accepted

Multiple choice substitute?

The implementation of :s//c is fixed; what you're looking for is replacement with a Vimscript expression (:help sub-replace-expr). You can query a single-digit via getchar(), or prompt for longer ...
Ingo Karkat's user avatar
20 votes
Accepted

How to run a substitute command on only a certain part of the line

Yes, this is a long answer. That's because I try to cover the sometimes intimidating sub-replace expression as clearly as I can. Don't be frightened...please dive in! :) This can be looked at as a ...
B Layer's user avatar
  • 20.1k
19 votes
Accepted

Substitute second occurence on line

In general you can match the Nth occurrence of something using \zs and \{N}. There's an example given at :help \zs. In your case the command would be: :%s/\(.\{-}\zstemp\.\)\{2}//
Antony's user avatar
  • 2,600
18 votes

How to replace each match with incrementing counter?

You need substitution with a state. I remember having provided a (/several?) complete solution for this kind of problems on SO. Here is another way to proceed (1). Now, I'll proceed into 2 steps: a ...
Luc Hermitte's user avatar
  • 17.7k
16 votes

How to find and replace in Vim without having to type the original word?

There are a couple more methods that (to my surprise!) haven't been mentioned yet. Using the gn Command gn works like the n command, except that in addition to jumping to the match, it enters visual ...
Rich's user avatar
  • 32.4k
15 votes

What is the most efficient way to invert two words?

Posting this so it can have an answer on our SE, but this is this source with full explanation. Here's a good way to use a single regex to do multiple substitutions: :%s/foo\|bar/\={'foo':'bar','bar'...
Tumbler41's user avatar
  • 7,786
15 votes
Accepted

What is the rationale for \r and \n meaning different things in s command?

At the most basic level, there's already an asymmetry between the search and replace portions of :substitute because the former is a regular expression and the latter is text, with specific additional ...
jamessan's user avatar
  • 11.2k
14 votes

Can I do an interactive :substitute (search & replace)?

My favorite interactive substitution is to search using / and go through the matches using n. Then select match using gn and then s to substitute the text with whatever I like. And repeat by moving ...
Neaţu Ovidiu Gabriel's user avatar
14 votes

Substitute second occurence on line

This is much easier done with sed: sed 's/\.temp\././2' With Vim you need non-greedy matching, and it isn't easy to extend the method to replacing the 3rd, 4th, etc. occurrence of temp. But it can ...
Sato Katsura's user avatar
  • 4,019
14 votes

Substitute second occurence on line

This solution is similar to TessellatingHeckler's but is more easily adapted to whatever pattern has to be deleted. :g/temp\./normal 2ngnd Here's how it works: :g/temp\./ for every line matching "...
frangio's user avatar
  • 241
13 votes
Accepted

Find and replace all numbers with 0?

Based on the help file, and the Vim Wikia page on Search and replace :%s/[0-9]\+\.[0-9]\+\|[0-9]\+/0/g :%s - start a command with the range of the entire file, %, to substitute, the following ...
Herb's user avatar
  • 1,213
13 votes

How to run a substitute command on only a certain part of the line

Since you mentioned visual mode... You can also use the \%V match to only match inside the current visual selection (or last one, if not in visual mode.) Using \%V would allow you to use the same ...
filbranden's user avatar
  • 29.6k
12 votes
Accepted

How can I execute multiple optional substitute commands in one line?

Add the e flag to your :s commands: :%s#from1#to1#ge | %s#from2#to2#ge From the flag's entry at :help :s_flags: [e] When the search pattern fails, do not issue an error message and, in ...
Martin Tournoij's user avatar
11 votes
Accepted

Is there a shortcut to reuse the search term of the find and replace?

Not exactly what you ask for, but you can do that: :s/foo/bar/g And then: :s//baz/g You can use that because when you use :s the searched pattern is saved in the search register. Which you can see ...
nobe4's user avatar
  • 16.2k
11 votes
Accepted

Suppress error message from "substitute" if string not found

You can silence individual commands inside a function by prepending :silent! just as well, so either :silent! execute or :execute 'silent! %s... would do. However, you don't need any of this, because ...
Ingo Karkat's user avatar
11 votes

Why does vim replace with confirmation only replace the first occurrence in the each line?

By default Vim replaces only first occurrence. You should add g to you substitution: :s/X/Y/cg. Vim will then let you change each X one by one (confirming with y) or all at once (confirming with a).
user1068352's user avatar
10 votes
Accepted

understand the need of "\v" in substitution

In this particular case, using \v doesn't make much difference s/^[^>]\+>\s//g is perhaps cleaner than using the very magic modifier Consider swapping two words: s/\(\w\+\) \(\w\+\)/\2 \1/ ...
Sundeep's user avatar
  • 1,096
10 votes
Accepted

Conditional substitution in VIM

You can use the replace special substitution: :s/./\= submatch(0) == '|' ? '+' : '-'/g Here \= says to use the following expression to use as the result string. And it's checking for submatch(0) (...
nobe4's user avatar
  • 16.2k
10 votes
Accepted

When substituting with :%s/old/new/g, does g flag have any effect?

The g flag to the :s command does not influence how many items are replaced in the whole file, but rather how often to apply the substition per line. E.g. :%s/foo/bar/g in a file like this: foo foo ...
Christian Brabandt's user avatar
9 votes
Accepted

How to make substitute() use another magic mode?

You can do this by including escapes in the pattern that change the magic mode. From :help pattern Use of "\m" makes the pattern after it be interpreted as if 'magic' is set, ignoring the actual ...
Geoff Reedy's user avatar
9 votes
Accepted

Replace ONE occurrence of something with MULTIPLE instances of something

You are probably best off just hitting dat Tab key 5 times in your example: :%s/\s\(item.\)/\r    TabTabTabTabTab    \1/g (The tabs display as ^I in the Command-line.) More elegantly, you can have vim ...
Aaron Thoma's user avatar
9 votes

How to replace bracket in vim?

You need to escape the opening square bracket: :s/],\[ /\r
Luc Hermitte's user avatar
  • 17.7k
9 votes
Accepted

How do I add text before and after the visual selection?

Use c and <c-r> to replace the text and "surround" it. From visual mode: c(<c-r>") This will surround the text with ( and ). Alternative: If you find yourself doing many "surrounding" ...
Peter Rincker's user avatar
9 votes

How to escape forward slashes?

You don't need to escape them: you could use a different separator for the search pattern and the replace part: %s;mvn;/opt/maven/bin/mvn;g But, if you really want to escape them, you can use a ...
João A. Toledo's user avatar
9 votes
Accepted

Replace nbsp with none string in utf-8 encoding in vim

You almost never need to deal with the binary UTF-8 encoding in Vim (or indeed, almost anywhere, including programming languages); for the most part you can just forget it exists as Vim takes cares of ...
Martin Tournoij's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible