Questions tagged [replace]

Overwriting text with the r or R commands. For search-and-replace operations, use the [substitute] tag.

Filter by
Sorted by
Tagged with
1 vote
1 answer
36 views

Remove needless repetition of words from a text including repeated words with apostrophes

This expression, run interactively within Vim: :%s/ \v(\w+('\w+)*)\_s\1/ \1/gi It removes needless repetition of words. In this way it turns this: the the it it them them it's it's then then and this ...
Kes's user avatar
  • 663
2 votes
4 answers
110 views

How to replace "," with ", " and saving the previous text?

I want to replace all occurrences in a text with "," for example "word1,word2,word3" became "word1, word2, word3" this one is bad :%s/,/, /g because "word1,word2,...
elbarna's user avatar
  • 179
1 vote
3 answers
321 views

How to print yanked data in replace mode?

I want to paste data from clipboard. But instead of insert the data, I want to overwrite (like using R).
Andy A.'s user avatar
  • 233
1 vote
1 answer
234 views

How to search and replace the highlighted text from visual mode?

The following is the functionality I am attempting to implement. 1. What works (normal mode): Search/highlight the word the cursor is on. Replace the word the cursor is on. Note that the word doesn'...
Vladimir's user avatar
1 vote
1 answer
71 views

How can I replace duplicate line prefixes with blanks?

Is it possible to transform: John Apple Red 3 John Apple Green 5 John Radish White 2 John Radish Red 7 Tom Apple Red 3 Tom Apple Green 5 Tom Radish White 2 Tom Radish Red 7 to: John Apple Red 3 ...
athos's user avatar
  • 195
0 votes
1 answer
441 views

How to replace one char in Neovim?

I used to use s for replacing one char with another. e.g. source: "bla". command: ss. result: "sla" source: "bla". command: 3ss. result: "s" Neovim (using ...
CIsForCookies's user avatar
0 votes
5 answers
448 views

Replace pattern with user input for every occurrence on the line

I'm a new Vim user, so please bear with me. A while ago I wanted to be able to replace a pattern with user input, and I stumbled upon this question on StackOverflow, and its answer worked beautifully. ...
Szpilman's user avatar
0 votes
1 answer
131 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 ...
Apoqlite's user avatar
  • 103
0 votes
3 answers
253 views

How to replace hex encoded string with chars?

I have UTF-8 encoded HTML with id(s) and href(s) encoded as hex values like %D0%B0%D0%B1 or .D0.B0.D0.B1. Can I convert such strings into readable form using Vim?
hobo-mts's user avatar
  • 123
0 votes
3 answers
126 views

Perform operation on multi occurence variable

I'm new to Vim and cursed coming from atom multi-cursor operation. I can't seem an easy way to change the variable names. Say I needed to replace bad_var only in the function - How would you do it? ...
nammerkage's user avatar
-1 votes
1 answer
15 views

Why the quote is placed at the beginning of string?

To match the string behind the : : TE: trailer The expression will convert the string into trailer: :1s/\v(.{-}):\s{1,}(.{-})/\2/ Why 1s/\v(.{-}):\s{1,}(.{-})/\2""/ convert the string ...
showkey's user avatar
  • 1,130
0 votes
3 answers
55 views

Strange result for grouping replace in a line

The file contains only one line: four_blank_space + "TE:" + one blank space + "trailers" + one blank space I write a grouping replace: :1s/\(\s\+\)\(.\+\):\s\+\(.\{-}\)\(\s\{0,}\)...
showkey's user avatar
  • 1,130
1 vote
2 answers
626 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 ...
Steven Lu's user avatar
  • 2,251
-1 votes
1 answer
34 views

How to replace between two marks with the content of a python variable

I'm writing a plugin to convert between word cases, and I would like to apply my filter between two marks. The conversion part is done with python, but I don't know how can I replace the text between ...
hl037_'s user avatar
  • 99
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?
yukashima huksay's user avatar
0 votes
1 answer
229 views

Replace inside matching parentheses

I want to replace all Box::new(x) with x, e.g. Box::new(A(B()) + C()) becomes A(B()) + C(). Does neovim (0.8.1) support that? I expect that there must be some feature for such a likely developer-...
minseong's user avatar
  • 2,323
0 votes
1 answer
110 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 ...
Charles Coulter's user avatar
0 votes
1 answer
23 views

Replace with fixed character (e.g. spaces) and add replaced text to register

I would like to have a selection that whose contents I replace with a command like r, but the replaced text is added to a register. The use case is editing a Markdown table in Vim, where I want to ...
Jonathan Jeffrey's user avatar
0 votes
4 answers
188 views

Replace every character on a visual selection with a register's character

I'll try to illustrate as clear as I can. Suppose I have the following text open in vim/nvim _________ | | | 9999 | | | --------- If select 9999 visually and then hit r8, this ...
Diwas10's user avatar
  • 115
2 votes
2 answers
306 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 ...
Alex Roberts's user avatar
1 vote
0 answers
18 views

How to swap every two words in the whole text?

I have such codes: int flag1 = GetWidget(widget); flag1.append(widget); delete widget; I'm willing to swap every flag1 and widget in the whole text, i.e.: int widget = GetWidget(flag1); widget.append(...
Mactarvish's user avatar
1 vote
1 answer
37 views

Replacement for ^M when scripting with Vim

I am creating a script that grabs all links from a web page's source code. I would like to call Vim from a bash script and the easiest way for me to do that is to use a vim script. I have a file ...
ex7lted's user avatar
  • 37
2 votes
1 answer
3k views

Delete all text before a specific character using vim

I have used curl to grab a website's code and put it in a text file. I'd like to delete all text that isn't a link! Lucky for me, all links are inside quotes, so I just need to delete all lines ...
ex7lted's user avatar
  • 37
1 vote
1 answer
54 views

Replace command with /n works wrong

I tried to count sentences in the text using the :s///gn command. It does no replace actually, just counts. The total command was: :%s/\C[A-Z]\_.\{-}[.?!]\_s\+//gn A sentence must start with a ...
Ilya Chernov's user avatar
0 votes
1 answer
18 views

target for number between two different special characters

I have the following string, where I want to change the number "merged_boris_20220712.boris". If instead of underscores it had dashes, I would just go to the numbers and do ciw and type the ...
fbence's user avatar
  • 677
1 vote
1 answer
132 views

Automate find and replace

When having selected something in visual mode where I want to perform a find-and-replace I always have to run :'<,'>s/foo/bar/g manually. Is there some way so that I can bind it to some key ...
Richard's user avatar
  • 155
2 votes
3 answers
781 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:...
user81371's user avatar
1 vote
1 answer
76 views

Using an inoremap with `r`

I have some inoremap's in my vimrc, such as inoremap \c <c-v>u2705 which enters a ✅ emoji for when I am writing checklists. However, I noticed that while I am able to use it when in insert mode ...
WalksB's user avatar
  • 507
1 vote
1 answer
491 views

How can I remove duplicates in comma-separated text, ignoring the first column, keeping only unique lines?

I have a sparse CSV file with data shaped like in this simplified example. I am omitting the header, Age,Fruit,Color,Count, but I can include it in my file if needed. You can see that the sparse rows ...
Kevin Duggan's user avatar
1 vote
1 answer
47 views

Replacing regex with italic code in Vim for Latex

I am using vim to write a document in Latex. I have to replace a regex with the italic code, which is \textit{regex}. I use the Vim code: :%s/regex/\textit\{regex\}/g But the \t expression is ...
Drosera_capensis's user avatar
0 votes
0 answers
224 views

Adding new line characters after a specific amount of bytes

I'd like to add new line characters after every specific number of bytes in binary mode. I tried this script but the repetition count depends on the number of characters not bytes. Like when there is ...
kirin's user avatar
  • 101
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 ...
tre332sju's user avatar
4 votes
3 answers
7k views

Find and replace with incrementing numbers

I have several lines that look like the following: "Timestamp": 200, "Timestamp": 1200, "Timestamp": 3100, I want to replace all the numbers, starting at 10 and ...
NH86Pen's user avatar
  • 41
1 vote
1 answer
46 views

How to only join lines that start with multiple spaces

what is the global command that will join lines that start with more than one space? I have tried this but it does not work as expected: :g/ \{2,}//J Example: I want to turn this: 01:001:001 In the ...
Jan P's user avatar
  • 31
3 votes
2 answers
516 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: /...
Rasmus Groth's user avatar
0 votes
1 answer
64 views

Key mapping for converting `((` to `\left( | \right)`

I would like to have something in .vimrc where when I type (( in insert mode, it automatically converts that into \left( | \right) with the cursor position at | in insert mode. Any ideas? I also would ...
Tom's user avatar
  • 379
1 vote
2 answers
541 views

Delete a line if it contains N occurrences

Is there a way to do a search and replace where it searches for if a line contains 4 pipes ( | ) anywhere in the line it will delete it? I'm working with a big csv file and some lines have 4 pipes and ...
Patoshi パトシ's user avatar
3 votes
2 answers
394 views

Is it possible to properly repeat a visual replacement?

It is possible in vanilla vim to: visual select some number of characters, e.g. a variable name in code type c to begin replacing selection in insert mode type a replacement (variable name) exit ...
Steven Lu's user avatar
  • 2,251
0 votes
1 answer
284 views

How to replace selected text T with some function(T), using a keyboard shortcut?

I want to annotate words /sentences for a "named entity recognition" purpose; inside a text file, I want to annotating words or sentences with this format [pattern](label): [a word where the ...
Giorgio Robino's user avatar
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 ...
Ramanjaneyulu Gudipati's user avatar
0 votes
1 answer
158 views

Can't find spaces in text with \s

I want to delete all the spaces in a document so I tried this: :s/\s//g but it did not work. What am I doing wrong?
Tyler Durden's user avatar
  • 2,101
2 votes
3 answers
523 views

Remove square brackets surrounding expression

In Python, I am trying to update my code from typed dict to dataclass. Language is just indicated as a reference, it is a pure Vim question. I need to replace my_params["some_property"] ...
Lionel Hamayon's user avatar
1 vote
1 answer
555 views

Replace two forward slashes with one using escape? [duplicate]

I am new to VI. How do you replace two forward slashes with one globally? I did: :%s/\// and :%/\//\/g Those don't work as desired. Example or Goal //This is good//because it//works; //on every//...
Kenny's user avatar
  • 13
0 votes
1 answer
245 views

Replacement of characters gives ^@ when replaced with newline but not newline [duplicate]

Why does replacement of something by using :%s/,/\n/gc in vim gives me ^@ instead of giving me newline. Sed replacement works perfectly sed s/,/'\n'/g. Any idea how to do sed thingy in Vim and save ...
Machinexa's user avatar
  • 103
0 votes
1 answer
60 views

Cycle through a list of letters in multiple groups - Code review

This is a follow up question to Cycle through a list of letters in multiple groups where I asked how to cycle a group of letters. I got a bunch of great tips and this is my follow up question. This is ...
N3buchadnezzar's user avatar
0 votes
2 answers
49 views

Cycle through a list of letters in multiple groups

So I have a case where I have some unicode letters say [B, Ḅ, Ḃ] and when I have my mouse selected over the B, (in normal mode) I want some way of cycling through the list. Like pressing some command ...
N3buchadnezzar's user avatar
0 votes
0 answers
29 views

Insert newline (\n) at search and replace but get "^@" instead [duplicate]

I want to set a linebreak after all braces }. I tried to replace the braces by newline \n with: :%s/\}/\n/ But all I got is a ^@ instead of a new line. Example: abc{def} foo bar{baz} and so on I ...
Andy A.'s user avatar
  • 233
1 vote
1 answer
31 views

replace simple pattern with sequential next line from another file

My file working.txt has 1000's of lines, which repeat in a simple pattern as follows: name date_ax name date_cd name date_ed name date_dc ...etc I need to replace each instance of name with the ...
Kes's user avatar
  • 663
1 vote
1 answer
584 views

Replace escape sequence `^[` carat left square bracket

Have text file with contents like: ^[[31morigin/some-branch^[[m ^[[31morigin/some-other-branch^[[m These ^[ sequences are highlighted and treated as one character as they are ascii escape sequence. ...
xxjjnn's user avatar
  • 123
0 votes
3 answers
1k views

search and replace in whole directory recursively

I have many entities in my java project like this: public class MyCar { // The brand of the car private String brand; // The time owner bought it private Date obtainTime; // The price in dollar ...
yz zheng's user avatar

1
2 3 4 5 6