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.
268
questions
0
votes
3answers
370 views
How to do the following replace in vim?
I want to replace all of the occurrences of
armv7_format_strings<any_string> = {<str1>, <str2>};
by
armv7_format_strings<any_string>[0] = <str1> ;
...
3
votes
3answers
2k 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 ; ...
3
votes
2answers
2k views
vim comment out every line of text containing a pattern using the 'global' command
In my file containing LaTeX source code, I wish to comment out all the lines matching a pattern (specifically, the pattern that I am interested in is \draw, including the \ character).
I have magic ...
1
vote
1answer
58 views
Substitute words for characters between matches in a line?
The raw text is like this:
<words> command feed world </words> tag=random
<words> who is that ? </words> tag=normal
I need to substitute words for characters:
<...
0
votes
1answer
571 views
Insert carriage return in a vimscript variable
A vimscript variable longstring (for instance obtained by let longstring = getline('.')) contains a long sequence of characters (numbers, letters, commas, spaces, ...).
I want to replace a specific ...
1
vote
1answer
76 views
Can you scroll the window in interactive substitute mode?
I am using an interactive substitution that spans multiple lines. In order to determine whether each particular occurrence should be replaced, I need to see all lines that are included in the search ...
0
votes
1answer
377 views
How to replace a character with another character by character code
I want to write a command that replaces all of a line's spaces with figure spaces, and I want the code to not use those literal characters so it is more legible than :s/ / /g
So, naturally, I try ...
3
votes
2answers
2k views
How do I get the first letter of a captured group?
The old three lines:
able
bee
must
I want to change them to this in vim:
able /home/words/a/able
bee /home/words/b/bee
must /home/words/m/must
I knew using %s/^\(.*\)$/\1/home/wprds/\1 can add ...
0
votes
1answer
49 views
Modify beginning of the line based on pattern at end of line
In file whenever I am coming across (int or string)
id","type": "int"},
name","type":"string"},
I want to replace with
private int id
private String name
Need VI command which updates private ...
3
votes
1answer
533 views
How to negate substitution pattern?
I've the following sample INI file:
Foo=1
Bar=1
Baz=1
Qux=1
and my goal is to set all options to 0 except Foo.
To disable one selected line, I know that I can run:
%s/Foo=\zs.*$/0/
Test command ...
2
votes
2answers
61 views
Why can't add a character at the beginning of each line in the edited article?
:set lcs=precedes:^,eol:#
I want to insert a character ^ at the beginning of each line.
Here is the result of :set lcs=precedes:^,eol:#.
Why lcs=precedes:^ can't add a character ^ in the article ...
5
votes
2answers
941 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
+-----------+----------...
0
votes
1answer
70 views
How to make substitute command respect different cases [duplicate]
Can vim substitute automatically depending on the case ?
like : s/foo/bar/ turns Foo to Bar and foo to bar ?
3
votes
2answers
6k views
Substitution: how to ignore the Nth first occurrences of a pattern?
I have a file that looks as follows:
aa,bb,cc,dd,ee,ff,gg
aa,bb,cc,dd,hh
aa,bb,cc,dd,ii,jj
etc.
I would like to (preferably using one command) change occurrences of a , by a : from the 5th , until ...
8
votes
2answers
207 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 ...
5
votes
2answers
10k 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
9
votes
1answer
1k 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 ...
5
votes
1answer
207 views
Replace ONE occurrence of something with MULTIPLE instances of something
How can you replace one match with multiple instances of a character (like \t)?
It's no problem to replace multiple characters with one.
But I did not find the answer for the opposite.
For example: ...
9
votes
1answer
958 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 ...
1
vote
2answers
807 views
How to call a macro as a function in a search replace %s/regex/\=call-macro-as-function/gc
I want to do some edits on each match during a global substitution operation.
I found this answer
:% s/re\(ge\)xp/\= myfunction(submatch(1))/gc.
But I don't have a function, just some keystrokes. ...
3
votes
1answer
731 views
Is there a nice way to remove semicolon in files using Vim?
Background:
My development team decided to move to typescript and one of the style guidelines was to remove all semi-colons and they will probably do so in newer JavaScript files (I know its a bit ...
7
votes
1answer
15k 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)?
-1
votes
1answer
43 views
How to do substitution in the selected part of a line instead of the whole line? [duplicate]
I want to do substitution in a partial of a line.
But when I type :'<,'>s/xx/yy/g, it always do it in the whole line.
How can I tell vim to do it in the exact selection area??
1
vote
1answer
317 views
What is the vim native way to apply math over pattern?
I need to re-factor a line with a repeated pattern in a way to apply some math over numbers. For e.g. let's assume the following line of code:
testpoints = [Point(1,2), Point(1,1), Point(1,2), Point(...
0
votes
2answers
128 views
How to execute global/substitution command asynchronously?
With the release of vim 8, I was wondering if there is a way to execute :s or :g asynchronously.
0
votes
1answer
462 views
Changing the type of brackets
I have a document containing these types of strings:
C(A,B)
C(D,E)
C(D,
E)
I am trying to replace every occurence of C(...) with \SC{...}, changing the C in front of the bracket and the brackets ...
7
votes
4answers
4k 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
...
2
votes
1answer
84 views
Get index of search in substitute expression
In a substitute command, is it possible to retrieve the 'index' of the search (i.e. for the n-th match, the index is n)?
I am looking to use this in this context: :s/pattern/\=[SEARCH INDEX HERE].
I ...
1
vote
4answers
2k views
How to batch replace a list of words in file?
Is there a way I can perform a batch-regex-replace using a list of words?
Simplified example:
List of words
Apple
Cabbage
Turtle
Text to search
Bike 2
Turtle 5
Bike 3
Bike 5
Cabbage ...
1
vote
2answers
153 views
Joining consecutive commas using ex-mode
I have a large list of scores that looks likes this:
Michelle
,46419
,32413
Frank
,46412
,33810
Steve
,28333
,17830
,37
Joe
,136
,93
Alicia
,121300
,84146
How can I move my list in Vim so it is re-...
6
votes
1answer
669 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() ...
15
votes
2answers
531 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
...
6
votes
1answer
268 views
How do I capitalize the first word after each period, question mark or exclamation point?
I would like to know the proper substitution command in vi that would accomplish this.
2
votes
1answer
428 views
How can I make two substitution command in one line
Giving these 2 substitution commands in vim:
%s/}$/\r}\r/g
%s/^\s*([a-zA-Z\#\.\:\s\(\)-\[\]'=]*)\s*\{/$1 \{/g
I have 2 questions:
How can I join 2 substitution commands in a one line command ?
Is ...
0
votes
1answer
797 views
Substitute words enclosed between comma or open-parenthesis and comma or closed parenthesis
I have lines like the following ones (actually function prototypes)
void ()
word ( word, another word, one_more word, ..., hello, ... )
one argument ( only )
I want to match each single argument and ...
2
votes
2answers
208 views
How to replace pattern in multiple lines with different replacements?
I have a line that fits a "template", and multiple pieces of "data". For each piece of data, I want to copy the template and replace all occurrences of a pattern in that line with the data. I'm using ...
0
votes
1answer
9k views
Substituting something with empty string? [closed]
How can I substitute something with empty string? So far the best I can do seems to be substituting with a whitespace. If I write :s/target// then it's deemed invalid.
1
vote
1answer
113 views
Text expansion for LaTeX editing
For writing a text book I would like text expansion; on typing this:
|25,50,65|
it should expand this to:
$25\ux,50\uy,65\uz$
I tried to use simple text replacement with :s%/TEXT/NEWTEXT/gc, but ...
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 ...
3
votes
1answer
154 views
Search and replace a visual selection in the whole file
I want to search and replace the selection I made in Visual mode. For example I have the following text:
Lorem ipsum dolor sit amet. Consectetur adipiscing elit. Sed quis gravida lectus amet.
And ...
2
votes
1answer
259 views
Substitution using groups from regex works only on one group out of three
I want to replace if (condition) //comment \n\s*{ (\n - newline, \s* 0 or more whitespaces), or rather restructure it, so brace is on same line as if and comment is moved to next one and is indented ...
1
vote
2answers
105 views
Substitution at the beginning of the text line, not the absolute beginning of line
If I select multiple lines and do substitution at the beginning of text it actually substitutes at the beginning of the line. For example, given:
abc
def
ghi
If we do:
Vjj:s/^/-->/
then ...
3
votes
2answers
14k views
Search and replace with multiple special characters
I would like to replace every instance of ... with the string \ldots, but both strings contain special characters. I have tried :%s/\<...\>/\ldots but this searches for all three character words ...
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 ...
1
vote
1answer
1k views
understand the need of “\v” in substitution
I have a following line:
<a href="/episodes/show-invisibles/"> Show invisibles: http://vimcasts.org/episodes/show-invisibles/
..and I have a :s/\v^[^\>]+\>\s//g substitution command ...
1
vote
4answers
133 views
Way to do mulitple substitutions on selection
When I visually select text and enter : I'll get this:
:'<,'>
Now I can enter a substitution to be done on those lines. However, what if I wanted to do several substitution, e.g. foo to bar ...
4
votes
2answers
103 views
How to change text in other line when the pattern is found?
I've got this 4-line content:
foo
baa
baz
quz
and I'd like to replace baa to bar only when quz is found.
I've tried these commands (which highlights baa correctly, but says it's not found):
:/quz/...
13
votes
11answers
9k 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, ...
8
votes
1answer
227 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(...
1
vote
1answer
89 views
Split a line by non-letter characters
Let say I have a minified javascript function like this:
function(){for(a=0;++a>0;){console.log(a);}}
and I want to apply an agressive split on it, that is insert a new line for some characters. ...