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
25
votes
1answer
10k 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 ...
10
votes
5answers
2k 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 :'<,'>...
19
votes
2answers
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 ...
16
votes
1answer
1k 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 ...
39
votes
4answers
11k 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 ...
15
votes
5answers
14k 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 ...
15
votes
4answers
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 ...
2
votes
1answer
186 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 ...
2
votes
1answer
608 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 /&...
68
votes
12answers
17k 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 ...
21
votes
1answer
211 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 :...
6
votes
3answers
4k 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 ...
16
votes
3answers
393 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?
15
votes
2answers
466 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 ...
13
votes
6answers
3k 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 ...
5
votes
1answer
807 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 <<...
4
votes
1answer
82 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 ...
8
votes
3answers
4k 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 ...
21
votes
5answers
9k 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 ...
8
votes
1answer
3k 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 ...
3
votes
1answer
307 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 ...
3
votes
3answers
3k 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 ; ...
8
votes
1answer
16k 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)?
6
votes
2answers
496 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 ...
5
votes
2answers
451 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 ---> </...
15
votes
3answers
8k 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 : ...
5
votes
5answers
4k 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 ...
4
votes
2answers
223 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 ...
4
votes
1answer
558 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? ...
3
votes
2answers
597 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 ...
3
votes
1answer
1k 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 ...
2
votes
2answers
2k 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” ...
2
votes
1answer
62 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-...
2
votes
1answer
41 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.
0
votes
1answer
67 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 ...