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
2answers
66 views

How do you :tag: every word of a line using substitution?

How do you substitute every word of a line, so that it becomes a useful vimwiki tag? Provided the following line: Word Compund_Word I want: :Word: :Compund_Word: I guess it involves some use of ...
3
votes
1answer
179 views

Unexpected substitution behavior

I need to put a comma between any two digits in a string of digits. For example, I want this 12345678910 to become this 1,2,3,4,5,6,7,8,9,1,0 The approach I took was the following s/\([0-9]\)\([0-9]\...
1
vote
2answers
48 views

Vim equivalent to Perl regex for making a substitution in a substitution?

I have this perl regex: s/(^(#+)\s.*?\g2)/$1 =~ s|\n| |rg/gmse It strips out all new lines between ##+ characters and replaces them with a space. It'll take a string like: before text more text ### ...
2
votes
1answer
70 views

Date change question: Find and replace with wildcards?

I have a file with a bunch of dates of the form a/b/c, where a,b, and c are numbers of varying character length. However, I'd like to change every instance to read c/a/b. I'm wondering if there is a ...
0
votes
0answers
31 views

Why \r means newline when replacing? [duplicate]

In vim's manual: When replacing: \r is newline, \n is a null byte (0x00). The \n's value is 0x0a in hex format,it is no relation with 0x00 ,what is the logic to define When replacing: \r is newline,...
5
votes
1answer
328 views

What's the meaning of the two question marks in substitution operation?

I'm learning how substitution works in Vim. And I find this example really weird to understand: :?a?,/b/s/bad/good. I know what this command will do in general. But why use two question marks to wrap ...
4
votes
1answer
38 views

Count lines matching a pattern

I'm trying to obtain the number of lines matching a pattern, from a function. I would like the cursor not to be moved at all during the process. In this case, non-empty and non-comment lines. Here's ...
0
votes
1answer
33 views

inserting a line number into substitution text

I have a long script file that I need to run in an application. I generally create these by running a query against the application and then creating the script I need using VIM. Sometimes these can ...
0
votes
1answer
110 views

Replace nbsp with none string in utf-8 encoding in vim

The nbsp(non breaking space)'s binary format in utf-8 encoding is c2a0. Create a sample.txt this way :input it is a test then ctrl+v+u a0,then you get the sample test file. xxd sample.txt 00000000: ...
0
votes
1answer
37 views

What does :s (by itself) do?

What does :s by itself do? I was thinking that it would be equivalent to: :s// Does it use the equivalent of <C-r>/ or something, or where is it grabbing the pattern from?
1
vote
1answer
8 views

Odd behavior when trying to perform a substitution on the clipboard register

I have a map that creates a markdown hyperlink for the current url open in the browser: inoremap <Leader>gg [<esc>:silent !~/bin/copy_safari_title.osa <cr>"+pa](<esc>:...
2
votes
2answers
44 views

Is it possible to make a substitution command that preserves the case of the matched pattern?

Consider the following constant definition: define("_TBL_USERS","users"); Now, I want to transform it into: define("_TBL_USER_ITEMS","user_items"); I tried ...
1
vote
1answer
29 views

Can we make substitutions based on rules from a file?

I've been using the command mode for making replacements in a file. Something like :%s/foo/bar/g and to make a series of replacements: :%s/i/1/g | :%s/j/2/g | :%s/k/3/g | :%s/l/4/g | ... This line ...
1
vote
1answer
24 views

How to insert text using substitute and regex?

Honestly I can't read regex for now. I have quick-fix list and I have a hunch that this can be done with regex but I don't know how. I want this: @ViewChild(MatTable) @ViewChild('text 1') @...
1
vote
1answer
24 views

Continuous number substitution

Suppose you have a textfile: Time step #1 (t = 0 a.u. = 0 fs) unneccessary text unneccessary text unneccessary text ... Time step #2 (t = 10 a.u. = 0.24189 fs) ... Time step #3 (t = 20 a.u. = 0.48378 ...
1
vote
2answers
89 views

How can I search/replace and retain a part that varies between the results?

What I'm trying to do is reformat some ugly JSON, replacing a pile of lines that are formatted: [whitespace][number],\n for example: 5, I have thousands of those lines, but don't know ...
0
votes
1answer
24 views

Includeexpr + substitute not behaving as I would expect

I am using includeexpr and substitute() in a ftplugin to remove a portion of an include string for gf functionality (in twig template files, for the curious), but I get an error instead. Twig ...
2
votes
1answer
72 views

Create a mapping in .vimrc that accepts an argument

I want to create a mapping in my .vimrc file that takes an argument. I'm constantly defining latex commands which require arguments inside of curly brackets and writing them instead inside regular ...
1
vote
1answer
39 views

Substitution within substitution? Replace with the result of a substitution operation on matched group

I would like to turn a list like this these are custom regex expressions 4 syntax? syntax weird one 3 there are many many many many words in this line 3 into this these are custom {4}regex {4}...
0
votes
0answers
26 views

Replace with another word on the screen

Sometimes I find it convenient to Search for text I want to replace /original Start writing a substitution to replace that :s/ Use C-r C-w to put the current word hovered into the command line :s/...
0
votes
1answer
12 views

substitute with modified matched with :s

I want to change : int i = libfnc1(); float j = libfnc2(); libfnc3(); to int i = lib::libfnc1(); float j = lib::libfnc2(); lib::libfnc3(); using :s I want to do somthing like :s/libfnc[0-9]/lib::*...
1
vote
1answer
32 views

using \r in {sub} of substitute() function call

I want to write a zsh vi-mode helper to wrangle complex commands but ran into a snag using \r in a substitute() function invocation. It results in \r replaced with control characters ^M in output. ...
2
votes
2answers
31 views

How to replay ex commands in order?

I often have a situation where I will direct some output to a file then open it in vim and use ex commands (substitution, keep lines, delete lines, etc) to massage the output to a form I want. However ...
1
vote
1answer
48 views

Is there any way to position the cursor in a :substitute replace pattern?

I have the following line: return err And would like to replace that to: return fmt.Errorf(": %w", \1) Which is done easily with something like: :s/return \v(\k+)/return fmt.Errorf(": %w", \1)/ ...
0
votes
1answer
18 views

How can I read a file and do template substitution?

Question How can I read a file into the current buffer and do some template substitution on it automatically? Background I'm working on a simple bash-driven logging / journaling capability. (on ...
1
vote
1answer
68 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 ...
2
votes
1answer
275 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
30 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="...
4
votes
1answer
59 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
630 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
543 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 ...
6
votes
1answer
119 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
43 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
182 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
345 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
154 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
113 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
250 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
90 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
397 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
423 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
58 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
285 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
22 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
68 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
424 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
426 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 ...
9
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
43 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\....

1
2 3 4 5 6