Questions tagged [replace]
Overwriting text with the r or R commands. For search-and-replace operations, use the [substitute] tag.
260
questions
0
votes
0
answers
9
views
How to search and replace the highlighted text from visual mode?
What works (normal mode):
Search/highlight the word the cursor is on.
Replace the word the cursor is on.
Note that the word doesn't get erased when I get prompted to replace it, allowing me to make ...
1
vote
1
answer
70
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
1
answer
196
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
243
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
67
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 ...
0
votes
3
answers
185
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?
0
votes
3
answers
100
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 ...
0
votes
3
answers
45
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,}\)...
1
vote
2
answers
298
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 ...
-1
votes
1
answer
30
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 ...
3
votes
1
answer
62
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
136
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
77
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 ...
0
votes
4
answers
152
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 ...
2
votes
2
answers
249
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
0
answers
17
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
33
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 ...
1
vote
1
answer
53
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 ...
0
votes
1
answer
16
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
111
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 ...
2
votes
3
answers
546
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:...
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
439
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 ...
1
vote
1
answer
45
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 ...
0
votes
0
answers
186
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
839
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 ...
4
votes
3
answers
5k
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 ...
1
vote
1
answer
39
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
501
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
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
2
answers
486
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 ...
3
votes
2
answers
319
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
258
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 ...
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 ...
0
votes
1
answer
133
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?
2
votes
3
answers
455
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"]
...
1
vote
1
answer
476
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//...
0
votes
1
answer
155
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 ...
0
votes
1
answer
51
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 ...
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 ...
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 ...
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 ...
1
vote
1
answer
495
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. ...
0
votes
3
answers
960
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
...
0
votes
1
answer
302
views
Increment number for every 10 lines
The file consists of 2 columns (code and percentage). We need to add 2 more columns with increasing number for 4th column (Xcols) and increment 3rd column (Ycols) only for every 10 lines with reset of ...
0
votes
1
answer
92
views
When searching and replacing can I store a capture group to a register?
This is similar but I am unable to draw the information I need from it: How to store all occurrences of a multiline search in a register?
I have these lines
################### trim 8 ...
1
vote
2
answers
112
views
Replace " ... " strings of various length with strings that all end at same column but start at different columns for each row
I have the following text
Plan based ................................. 32
Iron based in Compound .............................. 34
Gallium Arsnide based III-V ............ 36
...
Indium ...