Skip to main content
Share Your Experience: Take the 2024 Developer Survey
110 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.6k
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.1k
25 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
  • 375
23 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,146
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,759
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
  • 17.9k
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
  • 19.9k
19 votes
Accepted

Vim global substitution doesn't substitute all matches in a line

Maybe you, or a plugin, had enabled the option 'gdefault'. This option adds the flag g automatically whenever you perform a substitution. So, when you manually add the flag g while 'gdefault' is ...
user9433424's user avatar
  • 6,148
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,580
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.4k
16 votes
Accepted

What is the best way to re-factor a variable name in vim?

You can just use the usual workflow search and replace: /original cwreplaced n.n. You can take also advantage of the gn motion: /original<CR> cgnreplaced<ESC> ..... cgn will change the ...
nobe4's user avatar
  • 16.1k
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.1k
15 votes
Accepted

How can I run :substitute on only part of the line and not all of it?

You could use the following solution: First use v$ to select the text from the cursor to the end of the line. Then use the following command: :s/\%V\ /_/g The \%V indicate to change the text in the ...
statox's user avatar
  • 50k
15 votes
Accepted

Perform previous substitution for different selection with single keystroke

I think you want to do @: which replays the last ex command. See :h @: Note that this command can be called from normal mode and from visual mode. Also in this case it is not useful but you can also ...
statox's user avatar
  • 50k
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,756
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.1k
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,009
13 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
  • 231
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
  • 28.9k
12 votes
Accepted

Replace Nth Occurrence Using Substitute

Peter's answer achieves the result that you want, but not necessarily the question that you asked. This regular expression will match the Nth occurrence of your PATTERN: \(.\{-}\zsPATTERN\)\{N} To ...
Zach Ingbretsen's user avatar
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.1k
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
  • 17.9k
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

How do I replace a character with its hex value?

In that way, printf expects a decimal number to be converted to a hexadecimal one. All you need to do is convert the character to a number as follows: s/\([a-z]\)/\=printf("%#02x", char2nr(submatch(0)...
Jair López's user avatar
  • 1,854
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.1k
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

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