Questions tagged [replace]

Command or mode to overwrite text. For search-and-replace operations, use the [substitute] tag.

Filter by
Sorted by
Tagged with
-2
votes
1answer
51 views

Replace in a loop

I have a buffer that contains this sentence : I go to a rendez-vous this evening. and I have this function : function MyFunction() let myList = ["rendez-vous"] let myFile = readfile(bufname(1)) ...
1
vote
1answer
19 views

How can I replace a word while keeping the original casing?

Basically: cat -> horse CAT -> HORSE Cat -> Horse I know this is underspecified (e.g. unclear what cAt should become). However, is there a canonical solution for this?
0
votes
2answers
46 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 ...
1
vote
1answer
36 views

Search and Replace - How do you change a specific string across syntactically similar files?

I'm attempting to write a Vim macro that will allow me to replace a single variable of a single service across multiple Icinga configuration files. Services are syntactically very similar so I ...
0
votes
1answer
26 views

Changing multiple columns of all lines that meet a search criterion

There are lots of posts about how to search and replace a single column of a file, using \%Nc syntax. I want extend this syntax to replace multiple columns in all lines that meet some criterion. For ...
1
vote
1answer
49 views

Search for Pattern “^M*”

My file contains multiple blocks looking like this some Test "^M ^M ^M "some more text I'd like to remove these passages (including the "), but can't seem to fine the right regex. EDIT: I already ...
2
votes
2answers
38 views

How to search and replace more than character and a string in vim

I know it should be easy, but somehow I don’t get it right. I've got a text with artist_id, album_id,... and want to replace them with just id. How do I do that? I tried :%s/[a-z]+_id/id/gc, but ...
2
votes
1answer
53 views

How to search & replace the word under cursor without changing position?

This question is related to Do a forward slash search but don't have vim immediately jump to the string? but is not a dup. I recently updated my Linux box (clicked on the "install updates" button ...
1
vote
1answer
43 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. ...
4
votes
1answer
49 views

search & replace: how to add a line feed to break up a line?

So, I have the following line where I would like to find all the {#}= and put them on a new line. Of course there can be more then 9 value entries and there is more text on the line, so I am thinking ...
1
vote
1answer
63 views

Trailing characters error in a substitution command

Suddenly simple find&replace stopped working in one particular case. I'm selecting the code-block in visual-mode, so the final command looks like: '<,'>%s/someStuff/stuff/gI I get the ...
2
votes
1answer
347 views

Can `r` be used for multiple letters?

Background I am new to vi/vim world and finished running the vimtutor several times. The setup In the tutorial we learn several items to correct or edit lines of text. r to replace the letter the ...
7
votes
6answers
2k 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 \...
0
votes
0answers
18 views

Refactoring Ruby imports

Is there any way in Vim reliably refactor (i.e. rename, move up or move down directory structure) Ruby’s require_relative statements? I’ve searched thoroughly and couldn’t find any plugin or CLI tool ...
3
votes
1answer
33 views

search for pattern and insert text that precedes the pattern from pattern1 to pattern2 exclusive

basically I have a bunch of lines of code that all match the format .\module.variable (), .\module.variable2 (), I want to take everything between the backslash and the space, before the parentheses ...
0
votes
1answer
51 views

How to replace entire buffer with the contents of a variable?

I am trying to replace the entire buffer with the output from an external command. Similar to :% !myprog, but only if the command succeeds: let output = system('myprog') if v:shell_error == 0 " ...
2
votes
1answer
95 views

Don't jump on search and replace

How do I get my cursor to not move to the last thing it replaced after a search and replace. No <C-o> or extra key presses. Is there some autocmd magic I can setup to listen on any substitute ...
1
vote
1answer
33 views

How do I map multiple replace commands in visual mode

I'm trying to edit my init.vim to allow quick, custom retabbing with one key. My current attempt is as follows (the replace text in the last command is a tab character inputted in insert mode): ...
1
vote
2answers
51 views

Keep next letter in replace mode?

If I'm using replace mode, but I want to keep one or a few characters in a row of the text I am replacing, is there an easy way to skip forward rather than retyping the characters or exiting replace ...
0
votes
1answer
62 views

Can I select and paste with a single operation?

Here's my use case. I have the following text: This is a [hyperlink](http://someurl.com). Now I want to replace the http://someurl.com text with a new URL in the default register (something I've ...
1
vote
3answers
75 views

Search for line and paste as column until next match

I'm wondering if this is possible, more for curiosity's sake, but also it might be useful to know. I have several ascii data tables and I want to combine them all into a single file. So I combined ...
3
votes
1answer
57 views

How to replace the first matching for each line with regular expression

Saying that I have such a file as below: <random_string>hello<random_string> random_string<random_string>hello<random_string> I want to remove the first square bracket and ...
0
votes
2answers
36 views

How do I map a sequence of letters in insert mode?

After trying to make this work: Map a sequence of letters in insert mode I can’t get the correct answer to work. Is it still working? Can anyone post a working .vimrc file? I want to get this: When ...
1
vote
2answers
54 views

Replacement in visual mode

I wrote a function for selecting a text region and replacing in it certain predefined characters: fun! Tstrepl() range s/\%Va/\="\u2227"/ge " 'a' to '∧' s/\%Vb/\="\u2228"/ge " 'b' to '∨' ...
2
votes
3answers
91 views

Replace a string with a variable containing newline characters

I have a file that looks similar to this: module.exports = { blank: { area: 'frontend', name: 'Magento/blank', locale: 'en_US', files: [ 'css/styles-m',...
2
votes
1answer
62 views

Substituting (*x).y with x->y

I am coding a C project and as I am unexperienced, everytime I needed to access the element of a pointer to a struct I typed (*x).y (where x is a struct and y an item), which I just realized could be ...
0
votes
3answers
133 views

How to search in beginning of line and add something at end of that line

I want to search lines beginning with certain string in Vim, and then add something at the end of file. I have tried the following but it removed everything instead of appending :%s/^12138/*...
1
vote
2answers
52 views

How to replace a string only if the line starts with another string

I need to find ONLY the lines that start with the string G1 Z, and then replace any occurrence on that same line of F1800 with a new value of F500. For example: G1 Z9.800 F1800.000 ; move to next ...
2
votes
4answers
129 views

Search for line, move to end of search string, then search+replace

(EDIT: In the original question, I had accidentally used \w instead of \<. Comments and answers with \w should be interpreted as using \< as well.) In my LaTeX source files, section headings ...
1
vote
1answer
35 views

How do I replace all occurances of a string with a large amount of text?

I modifying a gcode file with custom toolswap codee, and I need to replace every instance of "T0" (^T0$) with a large amount of text (19 lines), which is stored in a separate file. How can I do so ...
1
vote
1answer
24 views

how to add a word two lines above certain word in multiples places

my text look like this in several places of the file: FDE PW91k XCNADD PBE ::FULLGRID END END eor and I want to introduce a word two lines above "eor" word throughout. I have tried :,.-2/eor/...
1
vote
1answer
388 views

Exclude folder when searching files in working directory

I'm working in a react project and opened vim from the root folder. When I'm using :vimgrep /pattern/ ** I just can search everywhere, but I want to search everywhere except the 'node_modules' ...
3
votes
2answers
719 views

How do I use 'cgn' for word under cursor?

I really like and often use the cgn command for replacing something I was currently searching for, but it sort of lacks a (imho) rather intuitive feature: replace the word (or whatever) under the ...
2
votes
1answer
199 views

vim argument substitution inside a function

I would like to use the arguments passed to a function, as an argument to replace a search. Something like this: function! Myfunction(...) %s/hello/ goodBye a:1, a:2/g endfunction Does anyone ...
1
vote
1answer
158 views

Find and replace in XML file in vi

I have an .xml file which contains many times the following web beacon <span lang="EN" class="multilang">ANY TEXT TOTO</span> I would like to make a "Find&Replace": each web beacon ...
0
votes
1answer
105 views

regex find and replace all with capture groups throws E488: Trailing characters

My text file looks like this Lastname, Firstname Doe, John I'm trying to get it to end like this using find replace regex Firstname Lastname John Doe This regex throws the error, although using ...
1
vote
0answers
32 views

Change a word with a N repating characters where N = word length

I have some code that I want to 'censor'. For example, I have secret token in a source code 1f85ez and I want to replace it with xxxxxx Now I do it with viwrx command. Is there way to make such a ...
2
votes
0answers
32 views

Substitute regex group in prompt when replace with confirmation

I am using vim to replace with simple regex: %s/Foo\(.\)/Bar\1Biz/gc I wanna to confirm each occurence and Vim asks: replace with Bar\1Biz (y/n/a/q/l/^E/^Y)? Is it possible to substitute group (\1) ...
1
vote
2answers
44 views

Replace all integers until a certain character

I am making a table which has chemical formulas in the first column so I need the integers as subscripts to use it in a LaTex file. I want to transform CH4 & to CH$_4$ &, C2H5 & to C$_2$H$...
4
votes
1answer
238 views

Replace With Text From The Line Above?

I have a text file like this: ge-0/0/45.0,First-Floor-Security,13 ,Voice_First_Floor,18 ge-0/0/46.0,First-Floor-Security,13 ,Voice_First_Floor,18 ge-0/0/47.0,AV-Media,155 ,Building-Automation,75 ,...
0
votes
1answer
86 views

How to replace seconds with hours:minutes:seconds?

With milliseconds; before: 64.173 I want to get: 00:01.04.173 Another example: from 23032.3 I want to get this: 06:23:52.3
3
votes
1answer
68 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 ...
1
vote
1answer
263 views

Adding a quantifier to group

I have the following file (I used 4 spaces instead of tabs here): abc123 abc123 abc123 abc123 abc123 abc123 abc123 kaskls abc123 kaskls The alphanumeric entries are separated ...
0
votes
3answers
66 views

Append `0,` to all lines having 2 commas and not more than 2 commas?

I have a file 0,3, 7,2,0, 1,2,3, 1,2, which should be 0,3,0, 7,2,0, 1,2,3, 1,2,0, So how can I append each line with two commas with 0,? I am interested in the most elegant solution and ...
1
vote
2answers
122 views

How to select a block in visual mode and delete this block and immediately enter insert mode?

for one single word, the job is quite easy: we just do a 'cw' what if I select a certain text and I want to replace this block with one word? obviously I can do a 'x' and then 'i', is there a one ...
2
votes
2answers
183 views

iedit behaviour in Vim?

For Emacs there is idedit. It Allows you to select some text, then hit one button, which automatically select all other occurrences of the selected text in the current buffer. Then you can edit the ...
1
vote
0answers
44 views

change ofWord to of Word, and ignore offer, office, off etc

I OCRed a document but has this problem: some spaces between "of" and the word after are gone and I need to add a space between "of" and "word". Is there any way to do it? And also to avoid adding ...
1
vote
1answer
37 views

In visual mode, how do I bind a key so that the selected text generates “%s/<SELECTEDTEXT>/<CURSORHERE>/g”?

In visual mode, how do I bind a key so that the selected text generates %s/<SELECTEDTEXT>/<CURSORHERE>/g ? Preferably ; considering I never use it otherwise. Thanks.
0
votes
1answer
147 views

Is it possible to return to normal mode in the middle of a vim command?

By simply calling :(range)g/bob/norm Axxx I can add xxx to the to the end of all lines in range containing bob. Is there a way to return to normal mode after having used A/I/a/i to enter insert mode, ...
1
vote
2answers
270 views

Delete from end of line in visual block mode

If i've got the code hello hellllo hello i can insert code before or after all of the lines by using I or A in visual block mode, respectively. How can I delete the last x characters? I'm aware ...