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
0
votes
1answer
164 views

Vim - find an occurence of a pattern one by one and and edit it at once

I would like to be able to find an occurence of a pattern, highlight it, go to the next occurence of the pattern, highlight it, go to the next one, and so on and so forth, and on the end delete all ...
1
vote
2answers
590 views

Search query to match multiple instances on a line, only between certain delimiters?

I do a lot of programming in Ruby, and a common edit suggested by Rubocop (a linter) is to reformat an array of symbols [:foo, :bar, :baz] as a percent string: %i[foo bar baz] I'm looking for a ...
1
vote
2answers
84 views

unable to replace text starting from *

I had following code:- Time::Time(short y,short m,short d,short h,short mi,short s): (*this).y(y), (*this).m(m), (*this).d(d), (*this).h(...
0
votes
1answer
76 views

How do I delete the first char of next line only?

On text like this #Comment #comment .... #comment #Comment I want to delete the # but only on next line,so become.. #Comment comment .... #comment Comment So far I do it with sed like this: ...
0
votes
1answer
233 views

Substitute only on certain lines

Is there a way to have s;/;|;g run only on the lines I filter using a regex? I have a file, which looks like: Date: 2017/08/18 Path /home/share/thing Path /home/me/thing Date: 2017/08/18 Path /home/...
1
vote
2answers
2k views

Find and replace string with slash in it and retain the slash in the new string

I am having a tough time finding and replacing a string using vi. Source String: &DB2_PORT;/&DB2_DATA; New String after replace: &DB2_PORT;/&DB2_DATA;:tcp=true I can find &...
1
vote
2answers
2k views

replace lines with the first and the last word

Here is a text with lines containing simple words: AZREG AREDB Z ARGHREHV AZRZEGV RYUJUS How to replace this line with only the first and the last word separated by a space? I have ended up with :! ...
-1
votes
1answer
108 views

Prepend "number," to each line, where number is calculated from nth term

I have a file with 48 lines like 0.00000 0.00621 0.16298 0.19977 0.40198 0.29450 0.49277 0.28054 0.43256 0.18729 0.28934 And I want to prepend d.ddddddd, to each line, each d represents a digit (so ...
1
vote
1answer
358 views

How to remove all words which doesn't match the pattern?

I've the following content: a1a foo bar buzz foo b2b bar buzz foo bar c3c buzz foo bar buzz d4d where I can match all words consisting only alpha characters by the following pattern: /\<[a-zA-Z]*...
1
vote
1answer
109 views

Quickly creating JSON entries in an Avro schema

Is there a quicker way to create more entries of form { "name": FIELD_NAME, "type": "string" } from a list of words down below, than recording a macro to process each word and then pasting the result ...
68
votes
12answers
19k 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 ...
1
vote
1answer
496 views

How to use the JS regex engine instead of the built in engine?

Is it possible to use the javascript regex engine instead of built-in one for search and substitute?
3
votes
4answers
488 views

How to get around this common multiple-cursors use case?

I have a lot of difficulty doing things like the following in Vim: From 1 'apple' 2 'orange' 3 'pear' 4 $fruit1 = 5 $fruit2 = 6 $fruit3 = To 1 2 3 4 $fruit1 = 'apple'; 5 $fruit2 = 'orange'; 6 $...
0
votes
1answer
677 views

Replace brackets and the content within the brackets in a variable

I'm currently stuck on this problem. I want, in a vimscript, to replace a variable's brackets when they contain a certain pattern. To say this differently, say I have a variable foo = "[content] bar" ...
1
vote
1answer
307 views

Copy a substring in line to the front of the line

I have a list of file names I'm trying to create insert statements out of. In each file name is the ID I need for the primary key, that I just need to copy from inside the file name to the front of ...
7
votes
1answer
821 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 ...
3
votes
1answer
1k views

Duplicate line and replace part of line multiple times

Considering this type of Json structure : [ { "id": "0000000000001", "en-US": "String 1" }, { "id": "0000000000002", "en-US": "String 2" } ... ] I ...
5
votes
5answers
5k 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 ...
1
vote
2answers
3k views

replacing urls and escaped urls

I am trying to do a search and replace on a large database file where some of the urls are escaped and some not, e.g.: http://www.example.com http:\\/\\/www.example.com I thought I could use a ...
0
votes
1answer
685 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: @@...
0
votes
1answer
256 views

Removing trailing spaces with the substitute command changes the cursor position

From http://vim.wikia.com/wiki/Remove_unwanted_spaces function TrimWhiteSpace() %s/\s\+$//e endfunction autocmd FileWritePre * call TrimWhiteSpace() autocmd FileAppendPre * call TrimWhiteSpace() ...
18
votes
5answers
16k 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 ...
2
votes
2answers
274 views

Substitute text on one line with capture group from previous line?

Suppose I've copied a bunch of lines of boilerplate code that I need to copy multiple times over, and then change in two separate places, in the same way: it 'includes sessions#new' do expect(...
1
vote
3answers
521 views

Convert a column of binary integers to hexadecimal format?

I have a text file with this format... 011000 001i 1111 011001 001i 0001 011010 001i 1111 011011 001i 1011 011100 001i 1111 011101 001i 1111 011110 001i 1001 011111 001i 1111 100000 001i 1111 100001 ...
0
votes
1answer
51 views

How to use regular expressions with \w\d inside ()

I created an example of text: dog cat worm And I want to replace everything but not the first word and without knowing what's there. At first I tried this::%s/\(dog\)\(.*\)/\1/g This works fine ...
0
votes
3answers
376 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
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 ; ...
4
votes
2answers
3k 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
59 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
699 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 ...
2
votes
1answer
99 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
478 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
81 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
703 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
66 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
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 +-----------+----------...
0
votes
1answer
80 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 ?
4
votes
2answers
8k 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
209 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 ...
6
votes
2answers
12k 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
10
votes
1answer
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 ...
5
votes
1answer
278 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
1k 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
1k 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
781 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 ...
8
votes
1answer
18k 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
368 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
143 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.