Questions tagged [search-replace]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
5 votes
1 answer
1k views

Search and Replace on odd/even numbered lines using g

I was looking for ways to search and replace on even/odd numbered lines in vim and I came across some solution on google groups https://groups.google.com/g/comp.editors/c/xk9DMoszgq4. To search and ...
1 vote
2 answers
102 views

:%s/xyz// is replacing every instance (without global specifier)

I thought that if no global specifier 'g' was used, then it was only supposed to do one replacement. For example, I thought :%s/xyz//g ' replace every instance of "xyz" :%s/xyz// ...
1 vote
1 answer
46 views

Is there a way to substitute a text that has regex formatted number ranges and replace with all combinations?

Let's say I have a file that looks like: host[1-2]lx host[1-4]win what I want is an easy way to get host1lx host2lx host1win host2win host3win host4win Is there a neat way to automate that process? ...
3 votes
3 answers
1k views

Use regex to substitute a match except the first and last characters

Maybe the title isn't to explicative; feel free to improve it. I have the text below: 1. Intro |timetracking-intro|$ 2. Uso |timetracking-usage|$ 3. Comandi |timetracking-...
0 votes
1 answer
46 views

Get index of search in substitute expression multiple times in Vim

Sorry. Although this is one duplicate question of this, but I don't have reputations now, so I can't comment on the above original question. As one workaround, I posted a new question and this is my ...
0 votes
1 answer
111 views

How do I include the match number when searching and replacing in vim using regex?

I'm trying to make my struct's members accessible through an index like an array, so if I have a struct like this in rust: pub struct SubjectsProgress { pub sword: i32, pub lance: i32, pub ...
-1 votes
1 answer
54 views

How do I substitute literal [ followed by whitespace, then comma with just [

I'm writing a vimscript function to edit some ruby code. The line to be edited looks like this foo = [ , "something" ] And I want to remove the extraneous leading comma, so the line ...
1 vote
2 answers
524 views

How to grab visual selection for search, but apply the appropriate escapes?

In this clip, From visual mode here after selecting im.g.mat I trigger my vnoremap <m-/> y:%s/<c-r>0//g<left><left> bind. The purpose of this bind is to initiate a global file ...
0 votes
2 answers
51 views

Trying to replace pattern _1_ with _01_ but its not working

I am using Vim version 9.0.1280. The search and replace command I am using is - :%s/\(_\)\([1-9]\{1\}\)\(_\)/_0\1_/gc When I run this vim offers to replace _1_ with _0\1_. What am I doing wrong?
3 votes
1 answer
63 views

Vimscript to replace persian/arabic digits with english ones

I have a file that has so many persian/arabic digits in it. I would like to replace them with their English counterparts. How is that possible?
0 votes
1 answer
106 views

Match/find and replace all series of tab characters with one tab

I have a tsv file that has the header separated by tabs but each row is separated by one or more tabs. The parser/viewer I'd like to use doesn't understand that the values are separated by a series of ...
0 votes
1 answer
36 views

Removing a line that ends with a " : " with vi (on AIX)

Very simply, I need to remove lines in a file that end with a :. I've tried many things but have not managed it. I cannot even find the right search pattern. example line: anbdskjhd: I would like to ...
2 votes
2 answers
292 views

Use *-like command in normal mode to execute a find/replace

Background It's nice to be able to type * in normal mode and automatically have Vim search for the word under the cursor. Question Is there an analogous command for executing a find/replace? E.g. the ...
0 votes
1 answer
42 views

Is it possible to use language mappings in search-and-replace?

Is is possible to use language mappings (such as those generated by the 'keymap' option or :lmap commands) in :substitute search-and-replace commands, the same way they get used in regular searches? ...
4 votes
1 answer
248 views

Upcase only the 3 and 4 letter words

I've written a script to convert 3 and 4 characters words, since they often are abreviations in the texts I work on. Here is the starting text : Diabetes - Hbp - Copd - I want to transform it to : ...
1 vote
1 answer
78 views

Replace arithmetic expression by their result

I am just trying to replace several arithmetic expressions inside a script file, which I cannot seem to find a simple solution for on the web. Adapting this answer did not do the trick. Suppose I have ...
2 votes
3 answers
701 views

Capitalize a match in search and replace

I am using the following regex to find the first letter of the last word on each line in a file: %s/.* .\zs/ I want to now lower case this match on every line in a file. How do I do it? For eg. Before:...
2 votes
1 answer
275 views

Keep specific parts of a :s command in nvim

What I'm trying to do is convert between Textile and Markdown links, so it should be able to do something like :s/".*"(.*)/[.*](.*)/, which is to say, keep the stuff that is matched by the .*...
1 vote
2 answers
178 views

Vim find and replace with filename as variable

Is it possible to use vim find and replace to replace a variable with the current filename? for example, fancyFileName.txt contains this is the VARIABLE to replace in this fancyFileName.txt and I am ...
1 vote
0 answers
17 views

Search and replace with regex in the replacement [duplicate]

I would like to replace all instances of match("something") with match("something", source). However, using :%s/match(.*)/match(.*,\ source)/g replaces it by match(.*, source). ...
1 vote
1 answer
48 views

add quotes to each string and change the delimiter for a comma

I have this string: private | public-read | public-read-write | authenticated-read | aws-exec-read | bucket-owner-read | bucket-owner-full-control I would like to find a mnemonic or easy pattern that ...
0 votes
1 answer
1k views

VIM search and replace regex - how to keep the search string intact and adding to it?

I'd like to learn how to search and replace in VIM - keeping the search string intact and adding to it. Take this as a simple example. See image below. I copied a simple python script written for ...
3 votes
1 answer
1k views

Match one regex pattern if not preceded by another one

I'm trying to match \item \macro{this\var}.% YYYY-MM-01 where 01 can be 1 and replace with \item The Month is MM \item \macro{this\var}.% YYYY-MM-01 when NOT this case: \item MonthYY \item \macro{...
0 votes
1 answer
596 views

Transform text that matches a pattern to uppercase

Searching /<pattern> in a document highlights text in some places like this: some text ... match1 some text ... some text match2 ... match3 text match4 ... Is there a way to make all the ...
3 votes
2 answers
508 views

How do I replace single whitespaces without affecting multi-whitespace such as indentation in Vim?

I have textfiles with paths that have lost underscores between words inline in an OCR process. I want to keep the indentation and put back the underscores in the paths. I have a path like this: /...
0 votes
1 answer
65 views

Interrupt a global search and replace with confirmation

Far too often, I've started a global search replace with confirmation, and discovered part way thru that I should have done a different search. Here's a silly example he he1 he the2 he them ...
0 votes
2 answers
203 views

Illegal back reference when trying to delete \1

I have a bunch of expressions (matlab anonymous functions) of the form, for example `f\^I = @(x,y,z) x^y + z`, `g1\^I\^I = @(z,b,c) z^b + c` (where \^I means a tab, but I can't just write that in ...