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
2 votes
4 answers
106 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,...
0 votes
3 answers
224 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?
1 vote
1 answer
184 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'...
1 vote
3 answers
318 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).
0 votes
3 answers
50 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,}\)...
19 votes
4 answers
12k views

How to replace more than one character without entering insert mode?

I have a file with a line that looks like this: foofoo.xy.barbar I want to change the xy to gh. I tried to do 2r -> gh, but obviously that didn't work, as it replaced the xy with gg and then moved ...
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 ...
25 votes
3 answers
16k views

Replace a pattern with current line number

I'd like to replace 'x' in this file with current line number. That is, change x x x to 1 2 3 For now, I use a Perl one-liner: perl -pi -e '$x=$.; s/x/$x/' myfile.txt I think UltraEdit can do it ...
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 ...
0 votes
4 answers
169 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 ...
0 votes
1 answer
323 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 ...
0 votes
5 answers
369 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. ...
0 votes
1 answer
108 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 vote
2 answers
496 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
3 answers
118 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? ...
-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 ...
1 vote
3 answers
3k views

Replace regex group in nvim

I'm using the most recent version of nvim on arch. I have sentences in my text like this The brown fox I wish to use this format :%s///g to replace all lines starting \nlowercase letter + rest of ...
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?
-1 votes
1 answer
31 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 ...
0 votes
1 answer
194 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-...
0 votes
1 answer
103 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
21 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 ...
5 votes
4 answers
1k views

Decode URL (percent decoding)

Other editors I have used are able to decode URLs. For example these can take a string like this: https://nim-lang.github.io/Nim/osproc#waitForExit%2CProcess%2Cint and produce this: https://nim-...
34 votes
5 answers
3k views

How to replace a word with a yanked word in normal mode?

When I have to replace a word with another word I yank before, I usually use a workflow which seems unnatural to me: Let's say I have this text: foo bar buzz And I want to yank foo and replace bar ...
0 votes
2 answers
222 views

How to replace spaces inside a selection

I have this text : He's a self made man who gets what he wants. The small blue forget me not flower was first used by the Grand Lodge. And this function: function MyFunction() let myList1 ...
2 votes
2 answers
288 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 ...
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 ...
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(...
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 ...
11 votes
3 answers
8k views

How to replace string with register content?

For example, substitute : with register 0's content. What's the correct command for :s/:/"0/.
1 vote
1 answer
36 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 ...
2 votes
1 answer
2k 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 ...
2 votes
3 answers
685 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:...
0 votes
1 answer
17 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 ...
1 vote
1 answer
75 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 ...
1 vote
1 answer
127 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 ...
1 vote
1 answer
530 views

Custom key binding for project-wide search and replace using FZF.vim Rg function?

I know that one can do a project-wide search and replace using the results of an Rg search in FZF.vim by doing the following: Run :Rg SEARCH_TERM Use Alt + a (or in my case, Esc + a, since I'm using ...
1 vote
1 answer
98 views

How to add a certain word before the names of methods in Javascript?

I am working with react. First I had a component with state I created methods and all that. Now I want to convert it into stateless component. I have add the word function before each method name. ...
1 vote
1 answer
472 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 ...
0 votes
0 answers
213 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 ...
0 votes
1 answer
282 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 ...
0 votes
1 answer
999 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 ...
13 votes
8 answers
10k views

How to replace a pair of brackets

I have a file, containing some lines like this: aaa bbb (ccc) ddd. Now I want to replace the pair of brackets by a pair of square brackets to make this line become aaa bbb [ccc] ddd. I think that \...
4 votes
3 answers
6k 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 ...
3 votes
2 answers
371 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 ...
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 ...
1 vote
1 answer
43 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 ...
3 votes
2 answers
507 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: /...
1 vote
2 answers
526 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 ...
0 votes
1 answer
145 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?

1
2 3 4 5 6