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
76 votes
12 answers
23k 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
  • 833
55 votes
3 answers
175k views

Find and replace using regular expressions

I have a file with a bunch of user defaults in. I want to change some of the text, but I'm struggling coming up with a matcher and replacer. Using the following example: ##############################...
squarefrog's user avatar
47 votes
5 answers
21k 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,083
29 votes
5 answers
14k 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
28 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.2k
23 votes
1 answer
262 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
22 votes
5 answers
20k 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
20 votes
2 answers
6k views

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 ...
theonlygusti's user avatar
  • 2,219
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
  • 789
18 votes
3 answers
12k 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,172
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
  • 49.3k
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
17 votes
5 answers
2k views

Replace a series of asterisk bullet points with a numbered list

Imagine I have the following text: some random stuff * asdf * foo * bar some other random stuff I want to replace the asterisk bullets with numbers, like so: some random stuff 1. asdf 2. foo 3. bar ...
Brennan Vincent's user avatar
17 votes
2 answers
3k views

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 ...
Nicolas Scotto Di Perto's user avatar
16 votes
5 answers
3k 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
  • 407
16 votes
3 answers
458 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,119
16 votes
2 answers
665 views

Multiple choice substitute?

Normally, a substitute with the c option gives me two options: (y)es or (n)o. Is it possible to have several numbered yes options to choose from? Example File person: Alice dog: Tobby person: Bob ...
Konrad Höffner's user avatar
15 votes
11 answers
16k 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
  • 15.9k
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.4k
15 votes
2 answers
511 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,749
13 votes
1 answer
2k views

Apply normal mode command to regex matches

I am using the following regular expression to match a documentation string underneath a Clojure function definition: \vdefn.*\n\s*\zs"([^"]|\n)*" Is there a way to run the normal mode gq (format ...
user avatar
11 votes
6 answers
2k views

How to append some line at the end of the file only if it's not there yet?

I'd like to edit a file in-place by appending a line, only if doesn't exist yet to make my script bullet-proof. Normally I'd do something like: cat >> ~/.bashrc <<EOF export PATH=~/....
kenorb's user avatar
  • 18.2k
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,679
10 votes
4 answers
415 views

Restricting operations to a section of code

There is many times in my day to day work life that I need to operate on a specific function, things like highlighting or searching for a variable, but I have never managed to limit the scope of the ...
Kyle Mcgill's user avatar
10 votes
1 answer
2k views

How to substitute in multiple lines?

I'd like to insert a word in multiple lines, is there any command like :[2,3,5,7,11]s/^/word\ /g which would insert word at the beginning of line 2, line 3, line 5, line 7 and line 11? Here, ...
user3813057's user avatar
  • 1,451
10 votes
1 answer
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 ...
elyashiv's user avatar
  • 2,429
10 votes
1 answer
2k views

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 ...
Rolf's user avatar
  • 265
10 votes
1 answer
2k views

How to get a preview for the substitution command?

Quite often I find myself in need of performing a substitution that will have matches sparsed all over a large file and with non-trivial search and replacement expressions. For example, say I have ...
Vitor Eiji's user avatar
9 votes
1 answer
358 views

Perform previous substitution for different selection with single keystroke

I performed following command :'<,'>s/2/digit/g on the code below manually visually selected: return _.round(this.getTotalAbs(), 2).toFixed(2); And it worked as intended return _.round(...
WitVault's user avatar
  • 1,679
8 votes
2 answers
218 views

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 ...
Eli Sadoff's user avatar
8 votes
2 answers
2k views

How to use substitution on register?

Let say I've register which has string stored in it, but I'd like to use substitute command to change it. For example I've 5x5 html table: let @t='<table>'.repeat('<tr>'.repeat('<td&...
kenorb's user avatar
  • 18.2k
8 votes
1 answer
22k 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
8 votes
1 answer
5k views

How to change last occurrence of the string in the line? [duplicate]

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 ...
kenorb's user avatar
  • 18.2k
8 votes
1 answer
2k views

How do I replace a character with its hex value?

I want to replace a character with its hex literal. For example before regex: h After regex: 0x68 I'm trying this: s/\([a-z]\)/\=printf("%#02x", submatch(0))/ But this replaces it with 00 ...
DJMcMayhem's user avatar
  • 17.4k
7 votes
2 answers
524 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
  • 49.3k
7 votes
2 answers
5k views

How to repeat last action with movement?

Assume the content: foo fnord bar;" >> /tmp/foo/derp gnarf foo fnord bar;" >> /tmp/foo/derp I want to jump to the " and delete to end of line and repeat that over the rest of lines. Yet ...
k0pernikus's user avatar
  • 2,957
7 votes
1 answer
4k views

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 ...
mxxk's user avatar
  • 227
7 votes
4 answers
7k views

Replace with hex character

Given this file: printf 'hello\x19world\n' > hello.txt I can replace a hex character: $ ex -sc '%s/\%x19/ /|x' hello.txt $ od -c hello.txt 0000000 h e l l o w o r l d \n ...
Zombo's user avatar
  • 1
7 votes
1 answer
1k views

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() ...
statox's user avatar
  • 49.3k
7 votes
3 answers
3k views

regex: how to replace "\["?

I have this sample line in the buffer: export PS1="\[${Cyan}\]$(((SHLVL>1)) && echo "${SHLVL}\[${IBlack}\].")\[${IGreen}\]\u\[${IBlack}\]@\[${Purple}\]\h\[${IYellow}\] \w \$ \[${Color_Off}\...
Kossak's user avatar
  • 1,638
7 votes
1 answer
203 views

How to prevent stopping a macro on unsuccessful substitution?

I've been trying to record a rather simple macro with two substitutions. The whole macro is: dd"+pkdd:%s/\t/, /g :%s/ \(kb\d\+\),/ \1.0,/g "+y$ However, the second substitution is there to reformat ...
VaNa's user avatar
  • 241
7 votes
1 answer
1k views

Replace all occurrences in visual block selection but NOT all occurrences on line

I'm trying to figure out how to do a search and replace on a visual block selection that will replace all occurrences in the visual block selection without replacing all occurrences on the line. What ...
user avatar
7 votes
0 answers
373 views

In a regex, why aren't the multi + and {1,} always parsed exactly the same way?

From :h /\+: \+ Matches 1 or more of the preceding atom, as many as possible. And :h /\{: \{n,} Matches at least n of the preceding atom, as many as possible It seems the multi + and {1,} have ...
user9433424's user avatar
  • 6,118
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
6 votes
3 answers
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 ...
Peter's user avatar
  • 217
6 votes
4 answers
10k views

How to delete part of a line that does not match a pattern?

I'd like to remove everything apart of my pattern match. For example I've the following line: window.page4_callback(["here \"is\" some text"]); and I'd like to leave the text only within the double ...
kenorb's user avatar
  • 18.2k
6 votes
4 answers
644 views

How can I store a series of regular expression search and replace commands and run them later?

How can I store a series of search and replace statements so I can run them later in sequence? Possibly after I have close and re-opened vim... For instance I might run the following in sequence ...
leeand00's user avatar
  • 3,457
6 votes
2 answers
5k views

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 ...
user18562's user avatar
6 votes
2 answers
1k views

Conditional substitution in VIM

Is there a way to set Negate in the substitution command? In other words how to substitute any character except | to - and all | to +. Example: | some text | other text | to +-----------+----------...
Ivan's user avatar
  • 336
6 votes
2 answers
16k views

How to replace bracket in vim?

I have "012-0738-32"],[ "4", "Beacon I want "012-0738-32" "4", "Beacon ],[+ space replaced with \r
alhelal's user avatar
  • 659

1
2 3 4 5
7