Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [substitute]

Any operation involving regular expression based substitution of text, such as the ex-mode `:s` command. Not to be confused with [tag:replace].

1
vote
2answers
120 views

How to add a space or a string into the substitute expression?

I am trying Vim capabilities and stuck with this task - addition the incrementing number to the end of each line. Testing lines: text text text text text This command works partially: :let n=1 | g/...
1
vote
0answers
20 views

search and replace: remember case in substitute

I have to change strings: fetchrandomData to fetchReportingData FETCH_RANDOM_DATA to FETCH_REPORTING_DATA I can change ca_z and CA_Z to ca_t and CA_T respectively with command: %s/\v([Cc])([Az])(_)?...
2
votes
1answer
71 views

How can I quickly replace multiple selections one after another?

To format text in LaTeX you need to surround it with curly brackets, e.g. \emph{} to render it emphasized. I often need to add commands like that to selected text fragments, nothing that could be ...
1
vote
2answers
38 views

Replace all integers until a certain character

I am making a table which has chemical formulas in the first column so I need the integers as subscripts to use it in a LaTex file. I want to transform CH4 & to CH$_4$ &, C2H5 & to C$_2$H$...
0
votes
1answer
27 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 ...
0
votes
1answer
35 views

Search and replace: Replace substring of match (with substitute(…))

I have a C program where I have to change a 2D array into a 1D array. What I want is to change this code: temp[i][j] = (Grid[i][j-1] + Grid[i][j] + Grid[i+1][j] + Grid[i][j+1])/5 to this one: temp[...
1
vote
2answers
22 views

Swap grid[X][Y] to grid[Y][X] in full file

In my file, I have many instances of grid[X][Y] where X and Y are variables. I would like to swap all instances to grid[Y][X] (preferably one by one so I can check them). Examples (especially note ...
2
votes
1answer
34 views

Search and replace: float arithmetic

For example I have many numbers like 111.222, I want to decrease them to 0.1 (to get 111.122), how to do that? I tried: :%s#\d\+\.\d\+#\=submatch(0)-0.1# But as a result I have 110.9.
1
vote
1answer
30 views

Suppress error message from “substitute” if string not found

I have this coding in my .vimrc: If any line contains trailing whitespace, then this function terminates silently, but otherwise I get this error message: I could add silent! to the autocmd, but I ...
3
votes
1answer
45 views

Replace all quotes with escaped quotes and vice versa

I am trying to replace all quotes (") with escaped quotes (\") and simultaneously all escaped quotes with normal quotes. I found the following solution to a similar problem here for swapping 'foo' to ...
0
votes
3answers
76 views

How do I replace all accented characters like “á”, “ő” and “é”?

I am trying to master VI and found some nice tricks to replace a letter with a single :%s/ command and this works. If I'd like to replace for example word with wardos I can use :%s/word/wardos/g and ...
3
votes
1answer
54 views

replace or operate within regex match

underlying question: Is there a way to operate on text within a regex match? I.e. is there an analog to the 'g' command that operates on match substrings instead of lines usecases: One example of ...
0
votes
1answer
37 views

Transform lines: prepend with incrementing number and string and append with string for each line in one step

I have the following lines: abc07 bca08 xyz17 And I would like to transform it to: [0]="abc07" \ [1]="bca08" \ [2]="xyz17" \ Based on this question, I can do the first part and the end ...
2
votes
1answer
36 views

Batch change name from ABC - DEF.pdf to DEF - ABC.pdf

I want to batch change 50+ files in a folder in Windows10. the original name style: 'how to install the software - softwareA user manual p1.pdf', the new file name will be 'softwareA user manual p1 - ...
1
vote
2answers
54 views

iedit behaviour in Vim?

For Emacs there is idedit. It Allows you to select some text, then hit one button, which automatically select all other occurrences of the selected text in the current buffer. Then you can edit the ...
1
vote
1answer
112 views

Copy first letter of each word in a line

I would like to copy the first letters of paper titles in a bib file and set that as entry name. Example: @article{Cqmdoprbcc, title={Can quantum-mechanical description of physical reality be ...
0
votes
1answer
37 views

Quick find and replace a fixed placeholder

I have a config file that have placeholder PKG, in workflow, I need to replace this PKG with a string but typing :%s/blah/blah/g cost time. I need a quick map key that asks for a string to replace ...
0
votes
2answers
56 views

Search and replace a pattern with new line except for a pattern

I am having a very long line file, I have to break the line into number of small lines. Example: < this>is a testing< /this>< next>testing< /next>< tested>successfully&...
0
votes
1answer
40 views

Vim multi-line substitute/insert once on selected text

I tried to make a function function! WrapSelect (front, back) "puts characters around the selected text. execute '''<, ''>s/\%V\(\_.*\)\%V\(\_.\)/'.a:front.'\1\2'.a:back.'/' nohl ...
0
votes
2answers
53 views

how to replace N characters without having to press <esc>, is it possible?

In command mode, r replaces the character under the cursor by the one pressed right after typing r without going into insert mode, so there's no esc to press. 5r replaces the 5 characters starting ...
0
votes
1answer
45 views

Replace a line with two other lines

I have multiple files with a string like: source /PATH/TO/OLD/ENV.sh And this needs to be replaced with: source /PATH/TO/NEW/ENV1.sh source /PATH/TO/NEW/ENV2.sh Usually, I use sed to replace ...
1
vote
1answer
54 views

Insert specific character after each instance of a character in a line

I would like to insert a specific character after each instance of another character in a line. For example, inserting a '\' after each instance of another '\' on a line. So, from this: C:folder\...
0
votes
1answer
667 views

Replace matching parentheses

Is there a way to match matching parentheses in vim regex? For example, if I have a line like this: function("test(with)parentheses"); // (comment) Command :.s/\(function\)(\(.*\))/\1[\2]/g ...
2
votes
1answer
51 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 ...
4
votes
1answer
343 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 ...
1
vote
2answers
43 views

Changing content within the specified [count] number of braces for each line

I need to change the content within a [count] number of braces for each line in my buffer. 1 {Lorem} ipsum dolor {amet} blah blah {change this text} more blah blah 2 Hello. The {sun} rises in {the} ...
0
votes
1answer
33 views

How do I substitute two same words (of different cases) to another two words whilst retaining their original cases?

I am sorry that my English was not good enough to clearly describe the task in the title. Here is a more descriptive attempt. Let us say I have the following text \gls{tla}{TLA} (As a side note, ...
0
votes
2answers
283 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” ...
0
votes
1answer
708 views

How to substitute string between two special characters?

Line pattern: abc : some-thing abc : some-thing; abc : some-thing --commment-: abc : some-thing; --commment-: Requirements: 1. Want to substitute the string between : and ;. 2. Comment is start ...
0
votes
1answer
55 views

Copy lines matching pattern, then substitute

I want to search file for pattern, duplicate every line that matches that pattern, then in perform substitution on each duplicated line. Suppose file looks like this: a 1 a 2 b 1 b 2 c 1 c 2 I want ...
0
votes
1answer
62 views

loading back-reference groups “shortcut”

I'm looking for a shortcut for loading back-reference groups using the same separator. Such a beast may not exist but it would be useful to me if it does. An example: Starting with this string :111 ...
-1
votes
1answer
78 views

Is \r operating system specific?

In the Vim documentation, it says that \r "matches < CR >", however for the result of replacement I could not find in the documentation a description of what it does. For example, imagine this: :%...
0
votes
2answers
42 views

Avoid so much escaping in substitution expression

I have a substitute expression to find words beginning with a dollar signs and capture them like this: :%s/\$\(.*\)\>//g However there are a lot of escape sequences in the regular expression (4). ...
0
votes
0answers
20 views

Rename occurrances of word? [duplicate]

The standard way in e.g. Sublime to rename some variable is to use multiple cursors. I know that in vim I can do something like :%s/foo/bar/gc to rename. However, to me it feels more appealing to do ...
2
votes
2answers
83 views

How to remove lines which have lower numbers than before, and keep only the sequential?

In order to parse certain log file and make a plot of sequential numbers, I need to filter out the lines with numbers which appearing lower than in the previous lines. For example, given the list of ...
0
votes
1answer
540 views

E488: Trailing characters error in VI with find and replace command

I am trying a simple find and replace function in VI Editor to change a profile path but the / is getting in my way and I need a simple and clever solution. I tried using: %s/profile: "/fldr0/local/...
0
votes
2answers
58 views

How do I write <8e>?

I extracted the text from a pdf which I need to edit further in vim. It contains quite a few diacritical combinations, some of them apparently got converted to control characters by pdftotext. It ...
1
vote
1answer
431 views

Finding all semicolons not preceded by backslash

I'm really struggling to figure out how to find all occurrences of a semicolon not preceded by a backslash. I'm hoping to find a quick vim command to clean up long bash commands that I've opened with ...
1
vote
0answers
49 views

parameter substitution template plugin

I would like to find a plugin, what can do the following in normal mode of vim: Parameter initialization Parameter substitution into text based on templates Suppose we have many sql query where ...
0
votes
3answers
64 views

Edit record register with multiple subtitution

I recorded a register and it work normally. I mistakenly overrode that register, fortunately I already saved the pattern. It's as below: V}y}pV}:s/_a\(\>\|_\)/_b\1/g^MpV}:s/_a\(\>\|_\)/_c\1/g^...
5
votes
1answer
155 views

Can vim perform all substitutions simultaneously?

The root of my question is about a difference between the way Vim processes multiple substitutions and they way other editors (Sublime, for example) do it. I have this file: thing1 thing2 thing3 ...
0
votes
1answer
47 views

Special search and replace

How can I search and replace array[j] by array in the whole script? I tried :%s/array[j]/array/ but that does not work. Thanks!
0
votes
1answer
35 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.
0
votes
1answer
89 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 ...
0
votes
0answers
36 views

Nicer mechanism for text body substitions

Frequently I need to do larger substitutions. An example would be changing an entire class for function. To do so I have to construct a large string with newlines to make the substitution. This can be ...
1
vote
1answer
91 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 ...
0
votes
0answers
55 views

Why did I have to repeat the substitution command several times to replace all instances of tab?

I was cleaning up a file I cloned from github that had several instances of tabs that I wanted to replace with spaces. I ran the following with only partial success: :%s/\t/ /g # globally,...
1
vote
2answers
42 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
74 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
71 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/...