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
58 views

Inserting a square bracket as part of search and replace

I have the text pattern [2] in a text file. I want to replace it with the text string '[1]'. Using the command: :%s/\<[2]\>/[1]/ only replaces the string '2' in the square brackets so the ...
0
votes
0answers
14 views

Is there an `edefault` in the spirit of `gdefault` for setting the appropriate flags in vim's substitution commands?

To perform a buffer-wide substitution on all occurrences of foo (and not just the 1st one in a line), we can do %s/foo/bar/g The g flag may be omitted by setting this in the vimrc as set ...
1
vote
1answer
35 views

Cycle between filenames

I have some files with names as below long-ordinary-filename-del-1.tex long-ordinary-filename-del-2.tex long-ordinary-filename-del-3.tex Is it possible to define a command to cycle between these ...
2
votes
1answer
37 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
102 views

Remove empty lines from a file

I'm able to find empty lines with /^$, but when I go to remove them using :%s/^$//, nothing seems to happen. Yet, some form of substitution is being made, because the status line reads XX ...
0
votes
2answers
56 views

Substituting without jumping

My problem is rather simple : I use a macro to wrap my current visual selection with some text (\verb; and ;, in fact). The current macro I use is :s#\%V.*\%V#\\verb;\0;. It works, but suffers from ...
3
votes
2answers
107 views

How do I replace '\\' with '\'?

Tried :s/\\\/\\/g but VIM will search for \\/\ instead. So is there any way I can do this in Vim?
0
votes
1answer
33 views

restrict shell command to only part of a line

In a file I'd like to replace some occurrences of triplets of 0 with F, 1 with E, ..., F with 0. Only some, so I can't just pipe the whole file through a bash tr filter; I need a text editor to ...
1
vote
1answer
20 views

word boundaries in abolish

:s/\<word\>//g works but :S/\<word\>//g doesn't How can I get equivalent of \< and co in abolish?
2
votes
1answer
37 views

Difference between searching and substituting pattern matching

I have a string in a JSON file which is repeated with different values for chance. I want to replace all of these blocks by moving the miss chance out of the attack array and into a separate property. ...
1
vote
3answers
78 views

Changing < to " but leave << alone in Vim

I have a .cpp file in which I would like to change all less-than symbols to double quotes but maintain any instances of double-greater-than symbols, i.e. change < to " while leaving all occurrences ...
1
vote
2answers
69 views

substituting /* in vimscript

I'm trying to remove the verilog block comment start: /* I'm trying to do this inside a function: call setline(line('.'), substitute(getline('.'), '/*','','')) Of some reason, it doesn't to ...
1
vote
4answers
59 views

Cut text with substitute into specific register & paste it back

As I see the substitution command e.g.: :% s/a/b/ puts a into the register "/ but the text that is being replaced is not saved in a register. Now, I would like to replace a with b but keep the ...
-1
votes
1answer
30 views

search replace for 1 line

I've been following how to do search replace in vim, but somehow it doesnt work for my case. For example, I have the following code: x(i,1,1) = y(i,1,1) + z(i,1,1) Now, I want to change the last ...
1
vote
1answer
41 views

How can I format a function declaration to force curly brace at the end of the line?

I have big portions of code where functions are formatted with the opening curly brace on the next line, like this : function foo() { } I'm trying to figure out how to search / replace in order to ...
0
votes
0answers
28 views

Search and replace any number separated by : trailing with number or alphabet separated by . and / or - or _

Search pattern examples "0:4.10.5-5.el7" "0:0.7.7-13.20120626.ov7" "6:4.14.8-6.po7_3" How to search and print all occurrences like above patterns where the first number can be any number ranging ...
1
vote
2answers
47 views

Search and replace only if following character is “x”

I'm trying to make a search and replace to format the following type of strings. INPUT someName0 : in std_logic; someName1 : in integer; someName2 : out unsigned; someName3 : out std_logic_vector(3 ...
0
votes
1answer
28 views

Is there a way to describe a repeating pattern in substitution?

I have multiple numbers in lines of 16, for example $02 c, $3f c, $fe c, $01 c, $ff c, $06 c, $00 c, $f8 c, $06 c, $1f c, $fe c, $00 c, $7f c, $8c c, $00 c, $fc c, with pattern [$][0-9][0-9]\s@, ...
3
votes
5answers
64 views

Replace all self-closing xml tags with open and close tags

I have an XML document with many (dozens) of the following tags: <item prop1="val1" prop2="val2" /> I would like to change this to: <item prop1="val1" prop2="val2"> <sometihng>...
0
votes
1answer
32 views

substitute lines in a function

In this function I would like to substitute everything within (" and ") with emptiness function! ListJoinFolds() abort 0 let bufnr = bufnr('%') let items = [] while 1 let ...
0
votes
3answers
125 views

Substitute tabs for equivalent number of spaces

N.B I use the word blank here due to lack of a better word. By blank I mean the remaining part of a tab that is visualized after the starting part. Please feel free to edit this question. I know ...
2
votes
1answer
70 views

vim argument substitution inside a function

I would like to use the arguments passed to a function, as an argument to replace a search. Something like this: function! Myfunction(...) %s/hello/ goodBye a:1, a:2/g endfunction Does anyone ...
0
votes
2answers
77 views

Replace tabs with appropriate number of spaces

I have this texts drwxr-xr-x enan enan Jan 15 16:10 Desktop/ drwxr-xr-x enan enan Jan 16 20:09 Documents/ drwxr-xr-x enan enan Jan 20 19:52 Downloads/ drwxr-xr-x enan ...
1
vote
2answers
146 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
27 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
75 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
43 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$...
1
vote
1answer
129 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
52 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
30 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
193 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
41 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
58 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
537 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
59 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
48 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
39 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
90 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
125 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
67 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
60 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
58 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
54 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
51 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
55 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
1k 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
81 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
765 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
46 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
38 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, ...