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

How do I add a thousand seperator to a number?

Is it possible to add a thousands separator to a number or a selection of numbers? Before 42 4815162342 After 42 4.815.162.342 I have to perform this operation quite often, which is why I would ...
1
vote
1answer
65 views

Replace multiple spaces with one

I want to replace all adjacent spaces with one space but the replacement should only take place if they aren't at the line begin. For example (I use brackets to demarcate line begin and end): [Hello,...
1
vote
1answer
29 views

Vim decremental replace not working

I want to replace the lines : https://www.dailymotion.com/video/whatever_1 # The Serie - S2 E4 - Grin and Bear It https://www.dailymotion.com/video/whatever_2 # The Serie - S2 E3 - Betty's Wait ...
1
vote
1answer
60 views

Is it possible to apply substitutions on parts of lines, instead of whole lines?

I have some text like this. a x="true" i q={1} That is, a line of text prefixed with 4 space characters. Then letters with spaces in between. I'd like to transform the text to this a x="...
2
votes
1answer
45 views

Case insensitive conservative substitution

Is there a built-in way in Vim to search for a pattern in a case-insensitive way, and substituting the occurrence conserving its case? (Something like an s/abcd/efgh that matches AbCd and replaces it ...
2
votes
4answers
606 views

Substitute figure by math

I have a file like this: 1 2 3 4 5 6 7 8 9 0 1 2 I want to change a block-wise visual selection area, e.g. 5 and 8. I want to substitute them by multiplying the figures by 0.7 and I want to ...
3
votes
2answers
490 views

how to increment a specific number in a csv file

I am very new to vim, and I have a csv file with this format : "1006","text:xxx" "2007","text:xxx" "3008","text:xxx" "4009","text:xxx" "5001","text:xxx" every "xxx" is replaced by an increment+1 of ...
5
votes
1answer
106 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 ...
0
votes
1answer
38 views

Search a term and replace with line numbers

I want to search and replace occurrences of $this->close_db(); with $this->stmt->close(); $this->database->close(); which occurs on lines 73-74 (with indentations). What is a ...
2
votes
3answers
126 views

How can substitute() operate on a whole buffer?

In normal usage :%s/A/B/g is a simple way to substitute a pattern across the entire buffer. However the command mode implementation of :substitute is subject to many user configurable variables and as ...
1
vote
2answers
153 views

How to replace a space with a colon - command line mode

Input Every line is separated into 8 parts by semicolons Every part can include words,numbers,space etc. Output I am trying to replace the space in the 6th part with a semicolon, thus separating ...
3
votes
2answers
132 views

How to use 2- or 3-letter :substitute commands?

:help change.txt mentions 2- or 3- letter :substitute commands, such as :sg, :sgn and so on. I am guessing that these :s[xx] correspond to flags, but am not sure how to use them. For example, I ...
1
vote
0answers
64 views

neovim hlsearch not working

I'm testing neovim and for many thing it is working way faster than vim (I use many plugins ~80, and I can really tell the difference with neovim). All the same config files worked out of the box, but ...
1
vote
2answers
133 views

Perform change to all occurrences of multi-line selection

Scenario I would like to perform the following, Visually select some text that spans multiple lines and occurs multiple times in the file: V Yank to buffer: y Perform substitute with text from ...
1
vote
1answer
77 views

Replace Only First Occurence after Pattern

Suppose I have a file which contains: TEMP=100 TEMP=100 and I want to change all of the string beyond 'TEMP=' to 200, so it would read: TEMP=200 TEMP=200 I could use the command :%s/TEMP=\zs.*/200/...
3
votes
2answers
141 views

Replace after a specific pattern

Suppose I have a string as follows: RESULT=100 How would I replace everything following 'RESULT=' so that the string reads: RESULT=200 The closest I've gotten so far is to use: :%s/\(RESULT=.*\)\@...
2
votes
1answer
180 views

Named capture groups in vim regex?

Does vim allow us to assign names to capture groups for search/replace that can be used in place of \1, \2 etc... for back-references and substitution? Like in in Python: >>> import re >...
0
votes
2answers
57 views

Find and replace only between searched patterns

Editing large HTML file. Would like to do something like: :/<blockquote>/,/<\/blockquote>/s/^\(\s\s\+\)\(<[^>].\{-}>\)/\1 \2/g So basically just trying to indent all between ...
3
votes
2answers
158 views

Using visual selection or register for search and replace

Is there a way to use a register or visual selection for search and replace? I have some longer sequence of words that are used repeatedly in my script. I want to replace them all but want to avoid ...
1
vote
1answer
20 views

Replace the regex search term with the new character along with the regex term

Consider the following text: alpha0_inc0 : beta0_inc0_term alpha1_inc1 : beta1_inc1_term alpha2_inc2 : beta2_inc1_term alpha3_inc3 : beta3_inc0_term I want to replace text matching the regex inc\d ...
1
vote
1answer
37 views

Search/replace multiple lines with confirmation

I'm trying to write a substitution command that would search for a line that doesn't end with a space character and others (see regex) and isn't followed by an empty line. The line length may vary ...
2
votes
1answer
233 views

How to escape forward slashes?

I was editing a file and found accidentally that I somehow escaped all the forward slashes in a path I was replacing in text. And that is something very useful! For instance, to do this replacement ...
2
votes
2answers
279 views

How to auto-replace words, such as acronyms, while typing?

When typing a report, we have a large list of acronyms defined in Acronyms.tex. In order to use it, we have to encapsulate an acronym such as KDF with the acronym command: \ac{KDF}. I often forget and ...
8
votes
5answers
1k 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 :'<,'>...
1
vote
1answer
41 views

Seach and replace with special characters

I am trying a search and replace because I am using some macros in my code. I want to replace tb.DUT.genblk4[0].U_rap_cisfd_wrapper this with `RAP_CISFD_WRAP_I in vim. I tried using :%s/tb\.DUT\....
4
votes
1answer
119 views

Replace from the current position to the end of the line

Is there a way to search and replace from a specific position in the line to the end of this line? I am here and you are there ^ I tried to show cursor position with ^. For instance, I don'...
1
vote
1answer
396 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
31 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 ...
2
votes
1answer
46 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
45 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
257 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
77 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
39 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
22 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
62 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
93 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
152 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
62 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
34 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
48 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
32 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
59 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
147 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
190 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
51 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
146 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
199 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
89 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
154 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
2 3 4 5