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
2
votes
1answer
28 views

Caret `^` and `\%V` compatibility in Vim regex

Can the caret ^ and the sequence \%V be used to match the beginning of a line in a visual selection? And if not, what is the rationale? The following example seems to indicate that ^ and \%V are not ...
-1
votes
0answers
50 views

How can I replace a line by line number?

Let say I have the following text hello world foo bar How can I change line 3 without changing my cursor? Right now I would do 3G cc to replace line with foo, but it will change my cursor position. ...
1
vote
3answers
137 views

Function That Substitutes Across Multiple Files

I'm trying to create a function that implements the functionality outlined here: Vim search replace all files in current (project) folder My current attempt: nnoremap <C-F> :call ...
1
vote
1answer
36 views

in-place replacement of code in HTML documents with TOhtml?

I'm working on some old HTML documentation, where are multiple code snippets (several lines long). I need to replace this old HTML-ized code with new one using modern styling. What I have now: ... ... ...
39
votes
4answers
11k views

How to replace only within visual selection?

How does one get vim to replace a character within the visual selection only and not elsewhere on the line? Example: Line: int arr[] = { 00 01 02 03 04 05 05 /* ... lots more like this*/}; Visual ...
1
vote
1answer
25 views

Replace two space indentation with tabs

I'd like to replace all two-space indents with tabs I can't do :%s/ /^I/g bc this replaces within lines too. I can't do :%s/^ /^I/g bc this replaces only the first two spaces of each line, not each ...
1
vote
2answers
81 views

Copy to clipboard or save to file substitution command

I'm trying to answer a vim question in U&L, which involves a very complex and long regex substitution. I would like to copy to clipboard the command, or save it in a file, so I don't have to make ...
5
votes
2answers
204 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
1answer
48 views

Configure Vim 8 behavior regarding substitution in visual selection

When I select a section of text within a line, then indicate I want to substitute on that line with :s, the result is that the entire line is affected. In order to actually do what I intend, I have to ...
10
votes
5answers
2k 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 :'<,'>...
2
votes
1answer
41 views

Why \%V is not added in visual mode during substitution by default?

\%V can be used to restrict match within the visual selection. However, I can't think of a situation when one would not want to do just that in visual mode.
1
vote
1answer
43 views

How to escape pipe (|) in mapped substitution?

I wanted to define a mapping for flipping the texts on both sides of a pipe (|) character. For example, substitute dům|Haus with Hause|dům The substitution command :s/\([^|]\+\)|\([^|]\+\)/\2|\1/ ...
68
votes
12answers
17k 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 ...
0
votes
1answer
23 views

Get count of string in vim

To get the count of a string I usually do something a bit hack-ish such as the following: Do a :%substitution on the string in question, such as: # how many times does the string "<Item "...
19
votes
2answers
2k views

Replace a string without changing case?

Due to a quirk in the domain-specific language I am working with, I frequently face the task of (selectively, not globally) replacing term or TERM with word or WORD, respectively. This means, I ...
3
votes
1answer
18 views

Replacing both upper case and lowercase findings [duplicate]

I often find myself running the following commands in sequence when wanting to replace both the lower case and title case instances of a given word, while retaining its original case. :'<,'>s/...
0
votes
3answers
4k 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 ...
1
vote
2answers
27 views

search last substitution

After doing :%s/foo/bar/g I can type :<C-r>/ to place foo into the command line. How can I place bar into the command line? I ultimately want to do a series of: :%s/foo/bar/g :%s/bar/baz/g :%s/...
0
votes
1answer
45 views

Substitution replaces only current line?

I have a block of of text which contains variables whose name I want to change, but not all. I want to be able to choose which variables get replaced by the substitution and which do not. I tried ...
1
vote
1answer
55 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)/ ...
4
votes
3answers
235 views

Recover whole search pattern for substitute command

Sometimes I type a long substitute command /%s/foo/bar.../ and while I am typing it or only after hiting enter I notice that I have mistakenly used a the search key (/) instead of the ex-command key (...
16
votes
2answers
2k views

Vim global substitution doesn't substitute all matches in a line

Sometimes my global substitutions don't substitute all matches in a line. It is confirmed by the fact that I can see it when I do substitutions with confirmation, a match is found in a line with ...
0
votes
2answers
139 views

Change date format from MM-DD-YYYY to YY-MM-DD using a regular expression?

A file contains 03-01-2020 04-01-2021 03-02-2020 04-02-2021 03-03-2020 04-03-2021 03-04-2020 04-04-2021 03-05-2020 04-05-2021 03-06-2020 04-06-2021 03-07-2020 04-07-2021 03-08-2020 04-08-2021 03-09-...
3
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
22 views

How to replace [ with ( in vim

I have this markdown line, ![Home][screenshots/home.png] I want to replace it with, ![Home](screenshots/home.png) So, I tried this, :%s/[s/(s/g Vim(version 81) says pattern not found.
-1
votes
1answer
63 views

Regex for any character that is not alphanumeric, whitespace, or a doublequote

I expect this to select all non-alphanumeric characters: [^\w] Instead it selects all non-w's. This is the regex I would use in JavaScript, Python etc.: [^\w\s"] How can I write this so that it'...
1
vote
1answer
32 views

substitution of increasing numbers per row

Each row is: Or(a=a[0], b=b[0], out=out[0]); Or(a=a[0], b=b[0], out=out[0]); Or(a=a[0], b=b[0], out=out[0]); and I need: Or(a=a[0], b=b[0], out=out[0]); Or(a=a[1], b=b[1], out=out[1]); Or(a=a[2], b=b[...
2
votes
1answer
21 views

Substitute: previous match with confirm

If I do a replace like this: :%s/May/June/gc afterward I keep hitting y or n as needed. But sometimes it hit n on accident and need to go back. I read that you can use N for this purpose, but it ...
0
votes
2answers
80 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
190 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
80 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 ### ...
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,...
3
votes
1answer
334 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 ...
4
votes
1answer
123 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
1answer
35 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 ...
5
votes
1answer
458 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
85 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
318 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
39 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>:...
3
votes
2answers
53 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
30 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 ...
2
votes
1answer
491 views

How can I make two substitution command in one line

Giving these 2 substitution commands in vim: %s/}$/\r}\r/g %s/^\s*([a-zA-Z\#\.\:\s\(\)-\[\]'=]*)\s*\{/$1 \{/g I have 2 questions: How can I join 2 substitution commands in a one line command ? Is ...
1
vote
1answer
31 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
29 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
103 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
45 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
192 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
42 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
48 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/...

1
2 3 4 5 6