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 ...
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 ...
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 (...
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. ...
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: >
:&&...
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 ...
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 ...
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}//
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 ...
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 ...
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'...
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 ...
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 ...
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 ...
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 "...
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 ...
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 ...
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
...
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 ...
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 ...
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).
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/
...
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) (...
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 ...
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 ...
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 ...
9
votes
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" ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
substitute × 355regular-expression × 100
replace × 31
search × 25
vimscript × 17
visual-mode × 17
neovim × 16
global-command × 15
key-bindings × 11
ex-mode × 11
command-line × 10
macro × 10
register × 9
vimrc × 6
cut-copy-paste × 6
cursor-movement × 6
multiple-lines × 6
find × 6
formatting × 5
repeated-commands × 5
indentation × 4
normal-mode × 4
functions × 4
whitespace × 4
line-breaks × 4