Questions tagged [substitute]

Any operation involving regular expression based substitution of text, such as the ex-mode :s command. Not to be confused with the [replace] tag.

344 questions
29
votes
5 answers

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

Sometimes I use :s with a complicated regexp which may or may not be correct, or I'm not sure if I want to replace all matches. Is there any way I can tell Vim to confirm every match it finds before it actually replaces the text?
Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
22
votes
5 answers

How to replace each match with incrementing counter?

I want to search and replace each occurrence of a certain pattern with a decimal number which begins at 1 and increments by one for each match. I can find similarly worded questions that turn out not to be about incrementing a counter but modifying…
hippietrail
  • 703
  • 2
  • 6
  • 16
20
votes
2 answers

How can I repeat last substitution and with the same flags

& repeats the last :s But it doesn't repeat with flags, e.g. a global sub: :s/"/'/g won't replace in the same way on consecutive lines with &, running :s/"/'/g on the first line here and then & on the second ["first", "second"] ["third",…
minseong
  • 2,313
  • 1
  • 19
  • 38
19
votes
2 answers

Replace a string without changing case?

Due to a quirk in the domain-specific language I am working with, I frequently face the task of (selectively, not globally) replacing term or TERM with word or WORD, respectively. This means, I search case-insensitively for term, and want to replace…
DevSolar
  • 789
  • 1
  • 7
  • 15
18
votes
1 answer

What does it mean to replace slashes (/) by exclamation marks (!) in a substitute command?

I recently saw a substitute command where the author had the / replaced by a ! like this: :s!abc!ABC!g I tried to find some documentation on this usage but I couldn't find anything relevant. So I tried to experiment by myself and once again I…
statox
  • 49,782
  • 19
  • 148
  • 225
17
votes
2 answers

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

Sometimes my global substitutions don't substitute all matches in a line. It is confirmed by the fact that I can see it when I do substitutions with confirmation, a match is found in a line with multiple possible matches, but then it goes to another…
16
votes
3 answers

Can I make :s operate only on the last match of the line?

:s/pattern/replacement operates on the first match and :s/pattern/replacement/g on all. How to operate only on the last one?
Toothrot
  • 3,129
  • 13
  • 27
10
votes
1 answer

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

I am comparing a couple of files with vimdiff. There are some differences which I expect and which I'd like to get rid of. I know I can use: :%s#from1#to1#g | %s#from2#to2#g | ... to replace multiple expected differences at the same time. However,…
Rolf
  • 265
  • 1
  • 8
8
votes
1 answer

How to change last occurrence of the string in the line?

I've the following example line: aa ... abc abc abc ... zz I'm aware that :s/abc/ok/ will change the first occurrence of abc in the current line and adding g flag will change all of them. How do I change last occurrence of the string (without…
kenorb
  • 18,433
  • 18
  • 72
  • 134
8
votes
1 answer

Suppress error message from "substitute" if string not found

I have this coding in my .vimrc: If any line contains trailing whitespace, then this function terminates silently, but otherwise I get this error message: I could add silent! to the autocmd, but I have other code in this function, for which I do…
nst0022
  • 496
  • 3
  • 7
8
votes
2 answers

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

I think there is a distinct possibility this question has already been asked, but I can't find it. If I am doing something like :s/foo/bar/g and then I want to call that again on the next line but with baz instead of bar, is there a faster way to do…
Eli Sadoff
  • 183
  • 4
7
votes
1 answer

How to make substitute() use another magic mode?

The doc about the substitute function :h substitute() tells us: [...] the matching with {pat} is always done like the 'magic' option is set [...] That's cool but what if I need to use substitute() with another mode like say, verymagic or…
statox
  • 49,782
  • 19
  • 148
  • 225
7
votes
2 answers

What is the most efficient way to invert two words?

Let's say that I have a file containing several occurrences of the word foo and several occurrences of the word bar. I need to make all the words foo become bar and inversely all the words bar become foo. What is the most efficient way to do…
statox
  • 49,782
  • 19
  • 148
  • 225
7
votes
2 answers

substitution in every other line

how do I search/substitute in every other line. :s/pattern1/pattern2{2}/g file looks like 1 pattern1 text 2 pattern1 text 3 pattern1 text 4 pattern1 text Output file should look like 1 pattern1 text 2 pattern2 text 3 pattern1 text 4 pattern2 text
user18562
  • 83
  • 1
  • 4
7
votes
1 answer

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

While working through vimtutor, I tried a variant on :%s/old/new/g by leaving off the g flag :%s/old/new and expected the substitution to only occur once in the whole file. After all, this mirrors the behavior of :s/old/new, which operates on the…
mxxk
  • 227
  • 2
  • 6
1
2 3 4 5 6