Skip to main content
New
Stack Overflow Jobs powered by Indeed: A job site that puts thousands of tech jobs at your fingertips (U.S. only). Search jobs

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.

Filter by
Sorted by
Tagged with
29 votes
1 answer
14k views

How to edit files non-interactively (e.g. in pipeline)?

I would like to edit files passed in pipeline input using vim in non-interactive way or edit files in-place (similar to sed). Few examples using sed: $ sed -i'.bak' s/foo/test/g file # Edit file ...
kenorb's user avatar
  • 18.6k
17 votes
5 answers
4k views

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

I have the following line in my code: INCORRECT_EMAIL_MOBILE_COMBINATION("incorrect_email_mobile_combination"); When I try to substitute the underscores with spaces in visual mode (using :'<,'>...
TheChetan's user avatar
  • 417
48 votes
5 answers
23k views

How to replace only within visual selection?

How does one get vim to replace a character within the visual selection only and not elsewhere on the line? Example: Line: int arr[] = { 00 01 02 03 04 05 05 /* ... lots more like this*/}; Visual ...
Bhargav's user avatar
  • 1,093
18 votes
1 answer
2k views

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 ...
statox's user avatar
  • 50k
19 votes
2 answers
2k views

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 ...
DevSolar's user avatar
  • 799
22 votes
5 answers
21k views

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 ...
hippietrail's user avatar
18 votes
4 answers
2k views

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

We all know that, when searching, \n is newline and \r is carriage return (^M), but when replacing \r is newline while \n is a null byte (^@). What is the origin of this asymmetry? Given that this ...
Matteo Italia's user avatar
1 vote
1 answer
1k views

How do I break a line with :substitute?

I usually have to worry about escaping search parameters to get special characters to match literally, but this time I'm seeing a problem with my replacement string. I want to take text like this: @@...
Mahmoud Al-Qudsi's user avatar
79 votes
12 answers
27k views

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

I'd like to optimize my "find and replace" workflow in Vim. It's something I do often, as I'm sure most of you do too. Usually something along the lines of -- copy a block and change the name of a ...
Joel's user avatar
  • 863
2 votes
1 answer
780 views

How to remove inner content of html tag conditionally?

I've html file (e.g. curl -o page.html http://example.com/) where I would like to remove inner html block for given html tag. I've the following code which works: $ ex -V1 page.html <<-EOF /&...
kenorb's user avatar
  • 18.6k
2 votes
1 answer
340 views

How to match pattern with buffer-scoped variable

Goal: write code to do block commenting/uncommenting while keeping the current indentation level. This has been solved, see here for my code Current problem: use s/// to match a line that begins with ...
jeremysprofile's user avatar
23 votes
1 answer
265 views

How does :g/^$/,/./-j (reduce multiple blank lines to a single blank) work in Vim?

In the article, Vim Regular Expressions, Oleg Raisky gives the following command to reduce multiple blank lines to a single blank: :g/^$/,/./-j Can someone please describe how this works? I know :...
user avatar
16 votes
3 answers
467 views

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's user avatar
  • 3,139
15 votes
2 answers
522 views

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

I have a line like this one: function my_function(argument, another argument, but there are spaces) I want to substitute the space characters (inside the argument's names) with underscore. I thus ...
Feffe's user avatar
  • 1,761
15 votes
6 answers
5k views

Delete all consecutive duplicates

I have a file that looks like this. Move to 230.00 Hold Hold Hold Hold Hold Hold Move to 00.00 Hold Hold Hold Hold Hold FooBar Hold Spam Hold I would like it to look like this: Move to 230.00 ...
DJMcMayhem's user avatar
  • 17.6k
6 votes
3 answers
5k views

Multiple search-replace operations on a selection

I have a LaTeX file with a bunch of commands of the form \label{<word>} where the <word> represents a string that contains special characters, namely á é í ó ú ý ð þ æ ö. I'm looking for a ...
Peter's user avatar
  • 217
5 votes
1 answer
178 views

Can I replace two distinct old strings with two distinct new strings using a single :s command?

Suppose I have the following line of text in vim: hello, it will be sunny today and I want to change it to: goodbye, it will be sunny tomorrow I could put my cursor on the line and type :s/hello/...
user38475's user avatar
5 votes
1 answer
689 views

Count lines matching a pattern

I'm trying to obtain the number of lines matching a pattern, from a function. I would like the cursor not to be moved at all during the process. In this case, non-empty and non-comment lines. Here's ...
Biggybi's user avatar
  • 2,760
5 votes
1 answer
959 views

Search and replace, but ignore text in quotes

As a poor-mans refactoring, I'd like to do a search and replace, but omit strings that are in double quotes (C/C++ quoting rules). For example cout << "Some variable " << variable <<...
jdm's user avatar
  • 151
30 votes
5 answers
15k views

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 ...
Martin Tournoij's user avatar
18 votes
3 answers
13k views

Substitute with pure vimscript (without `:s`)

I have the following in my vimrc: func! AddSpaceBeforeEqual() s/\([a-z)_0-9"'\[\]]\)=/\1 =/ge endfunc I'm using vint to lint my vimrc, and got the following warning : ...
edi9999's user avatar
  • 1,192
16 votes
11 answers
17k views

Substitute second occurence on line

I have a list of files: ./a.temp.txt ./a.temp.txt ./a/b.temp.txt ./a/b.temp.txt ./a/b/c.temp.txt ./a/b/c.temp.txt And I want to remove the temp. on each line, but only the second occurence, ...
nobe4's user avatar
  • 16.1k
10 votes
3 answers
7k views

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

I know there is one way to search and replace variable occurrences across a source file. But is there any better way by which if I rename a variable at one place it gets renamed at other places as ...
WitVault's user avatar
  • 1,719
10 votes
1 answer
4k views

How to translate Unicode escape sequences to the Unicode character?

I have a file with a lot of characters represented like this: \u05E2. (These are the actual characters in the file: backslash, lowercase u, and four hexadecimal digits.) Due to that, the file is ...
elyashiv's user avatar
  • 2,429
8 votes
1 answer
23k views

Find and replace all numbers with 0?

Is there a command to find and replace all instances of a number in a file with 0 (including decimal numbers like 1.7)?
user1770201's user avatar
7 votes
2 answers
537 views

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 ...
statox's user avatar
  • 50k
6 votes
3 answers
4k views

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

Suppose I’ve selected some text in visual mode. How do I insert a certain piece of text at the beginning of the selection and a different piece of text at the end? (I’m only concerned with “normal” ...
bdesham's user avatar
  • 2,983
6 votes
2 answers
2k views

Using visual selection or register for search and replace

Is there a way to use a register or visual selection for search and replace? I have some longer sequence of words that are used repeatedly in my script. I want to replace them all but want to avoid ...
M.O. Kitzka's user avatar
6 votes
5 answers
6k views

Match any number of digits NOT followed by

I want to search for: From start of the line, any number of white spaces followed by any number of digits NOT followed by a dot. And replace it with all that was searched and a dot. This is just ...
deshmukh's user avatar
  • 265
5 votes
2 answers
481 views

Substitution with math goes wrong

I have a large xhtml document that is broken into some 245 long paragraphs, each with a unique id in the form of "ud###" <p class="windowbg" id="ud000"> <!--- lots of content ---> </...
FitzmorrisPR's user avatar
4 votes
1 answer
2k views

How to substitute the first occurrence across the whole file?

I've file in which there are some repeated parts and I want to get rid of the first occurrence. Normally I'd do %s/_exclude// to perform substitute for all lines, however I want to replace only the ...
kenorb's user avatar
  • 18.6k
4 votes
1 answer
411 views

Insert subsequent numbers in a substitution pattern

I want to insert subsequent numbers whenever there a regex matches. I do this in terminal using for command and either perl or sed. Is there a way to do this in vim? Basically I want this: This ...
One Face's user avatar
  • 243
4 votes
2 answers
277 views

How do I remove lines where its content already occured in the previous line?

For example I've the following text: This is my first line, but longer. This is my first line This is my second line, but longer. This is my second line This is my third line, but longer. This is my ...
kenorb's user avatar
  • 18.6k
4 votes
1 answer
666 views

Substitute all except the last asterisk in groups of two or more asterisks

How can I substitute all except the last asterisk in groups of two or more asterisks with two spaces? E.g. * Micro topic 1 ** Microbes are small ** You can't see them! *** Isn't that something? ...
truthling's user avatar
4 votes
3 answers
6k views

How to truncate every line after pattern

I just don't undstand the VIM help for pattern matching and substitution. I've tried. I have many lines of code, like so: G1 X139.164 Y115.348 E8.40357 ; perimeter G1 X138.903 Y115.845 E8.57778 ; ...
Davo's user avatar
  • 176
3 votes
2 answers
86 views

Why \%V is not added in visual mode during substitution by default?

\%V can be used to restrict match within the visual selection. However, I can't think of a situation when one would not want to do just that in visual mode.
tejasvi88's user avatar
  • 470
3 votes
1 answer
1k views

How to escape pipe (|) in mapped substitution?

I wanted to define a mapping for flipping the texts on both sides of a pipe (|) character. For example, substitute dům|Haus with Hause|dům The substitution command :s/\([^|]\+\)|\([^|]\+\)/\2|\1/ ...
Hermann Schachner's user avatar
2 votes
1 answer
94 views

Alternate vim mapping

I have a file that looks something like this \usepackage{tex/Forkurs-1p-2p-laererutdanning-2018-V} \usepackage{../Sty/hyper} \begin{document} \input{tex/Forkurs-1p-2p-laererutdanning-2018-V-...
N3buchadnezzar's user avatar
1 vote
1 answer
76 views

How do you substitute on previous matches?

For example, if I have the following: MY_VAR = "hello world" print "the var %s" % MY_VAR If I position the cursor on MY_VAR and press *, Vim highlights both matches. Suppose I want to rename this to ...
squarefrog's user avatar
0 votes
1 answer
206 views

What is the syntax if I want to replace one directory with another in vim given that I have '/' in it?

If I have this directory: /home/abc/def/apples/ and I want to replace it with this directory: /home/nfl/nfl/nba/ in vim, what is the syntax? I tried using %s/a/b/g but this command fails.
Charley Degrasse's user avatar