Questions tagged [replace]

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

Filter by
Sorted by
Tagged with
0
votes
2answers
41 views

VI replacing 1 character between two double quotes

I have a 200,000+ line file that I need to update. My issue is that I need to use commas to separate the fields; however in the original file there are commas placed in the "comment section."...
3
votes
1answer
34 views

Need a functionality behaving like Replace on text but for Pasting Blocks

One of my vim usages is to do sketches of topologies in networking I've found the script Draw it really really useful for that So I can draw things like this by pasting these boxes +------+ |ROUTER| ...
3
votes
1answer
37 views

How to move a csv column

Suppose a classic csv like this (each number represents an alphanumeric sequence) : 1,2,3,4,5,6,7 1,2,3,4,5,6,7 1,2,3,4,5,6,7 I was trying to move the 6th column of each line to the beginning of the ...
1
vote
1answer
67 views

Replace a character multiple times but within pattern

I am creating a yml file out of some variables. I have like 80 lines and they all have the pattern: my-first-command-line-var: {{ my-first-command-line-var }}, my-second-cli-var: {{ {{ my-second-cli-...
2
votes
3answers
311 views

Select multiple words, one at a time, then replace them all

Goal: Select multiple words, one at a time, then replace them all. I'm new to Vim; I come from Visual Studio. I would like to achieve one important feature that helped me a lot in my productivity: ...
0
votes
1answer
30 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
1answer
30 views

How to switch the order in which objects appear

I have a custom command in a TeX file which takes two inputs A and B, let's call it \operation{A}{B}. I want to find all of the instances of \operation where the first entry A starts with the string ...
0
votes
3answers
55 views

Is it possible to exchange search patterns in vim?

Is there a way, built-in or with a plugin, that I can swap any number of two different patterns on an arbitrary range using a regex? Maybe something :s-style like :{range}exchange/{pattern-1}/{pattern-...
2
votes
1answer
42 views

text replace problem when target contains '&' symbol

I tried to replace if(cond1) with if(cond1&&cond2) in vim, so I typed the command :%s/\Vif(cond1)/if(cond1&&cond2)/g but the result is if(cond1if(cond1)if(cond1)cond2) and not if(...
1
vote
2answers
25 views

How to replace everything between PORT= and )

I have a (copy of) oracle listener log, and would like to replace the portnumber in every line. These are always of the form (PORT=1234) with sometimes 4 and sometimes 5 numbers. So an exampleline, ...
4
votes
4answers
104 views

Plain search and replace like Notepad?

I am aware of the substitute command and of its usage as an ex command :%s/<old>/<new>/g However when <new> contains a very complicated string, say a complicated URL with lots of ...
3
votes
1answer
152 views

Date change question: Find and replace with wildcards?

I have a file with a bunch of dates of the form a/b/c, where a,b, and c are numbers of varying character length. However, I'd like to change every instance to read c/a/b. I'm wondering if there is a ...
0
votes
1answer
34 views

inserting a line number into substitution text

I have a long script file that I need to run in an application. I generally create these by running a query against the application and then creating the script I need using VIM. Sometimes these can ...
0
votes
2answers
62 views

Define a new ex command active only in specific filetype (C++)

I would like to define a new command for C++ files/buffers (ft=cpp). I shall explain the requirements through the example below. The newly defined command :stripdbg should strip the surrounding dbg() ...
1
vote
1answer
29 views

Can we make substitutions based on rules from a file?

I've been using the command mode for making replacements in a file. Something like :%s/foo/bar/g and to make a series of replacements: :%s/i/1/g | :%s/j/2/g | :%s/k/3/g | :%s/l/4/g | ... This line ...
5
votes
1answer
73 views

Vim find/replace highlighting

I'm a long time Vim user but a few months back started dabbling in Emacs. One of the things I really liked about Emacs was when doing a find/replace s/you/me it would highlight the word I was ...
2
votes
3answers
69 views

How can I repeat the cgn sequence?

Recently I learned the awesome command cgn to repeat a change to a word. I’ve seen several answers advising its use, like this: What is the best way to re-factor a variable name in vim? and this: How ...
2
votes
3answers
1k views

Select WORD under cursor

I know that * selects the word under the cursor, but what if I want to actually select a WORD ? Just to be clear, I would then like to use the selection for a replace, something like :%s//foo/g
2
votes
1answer
15 views

Change words but retain format and character casing

What is the most convenient way to do search and replace for same words in multiple formats. I often find myself doing tons of regular expressions as follows. :%s/scubaDiver/deviceDriver/g :%s/...
0
votes
1answer
32 views

global search and replace on all lines that don't begin with a backslash

I'm trying to globally enclose in {}'s all instances of +,-,= that aren't already so enclosed. But I don't want to do this for lines that are latex commands. Since my latex commands are all on ...
1
vote
1answer
24 views

How to insert text using substitute and regex?

Honestly I can't read regex for now. I have quick-fix list and I have a hunch that this can be done with regex but I don't know how. I want this: @ViewChild(MatTable) @ViewChild('text 1') @...
0
votes
1answer
39 views

How can I create mappings for replace mode?

I know how to create key mappings in normal mode using nmap and in insert mode using imap. However I do not know how to create mappings in replace mode. I would assume it's either rmap or Rmap, but ...
1
vote
3answers
113 views

How to delete everything except urls?

I have string like this: 1668955,,, bunch of text</figcaption><br /><a href="https://the_url/article_detail.php?article_id=10003414" target="_blank">w..... </a><br />&...
1
vote
4answers
29 views

Vim invert block of text over an axis

I was wondering what would be the most Vimish way to the following: I want to invert the text on the right side of the equal sign to the left (and the left to the right). For example, I want this: v1 ...
11
votes
10answers
1k views

Simultaneous find and replace

Is there a way to perform a simultaneous find and replace? e.g.: I want to replace all 1 for 0 and simultaneously all 0 to 1 [ [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 0, 1], [1, 1, 1, 0, 1, 1], ...
0
votes
2answers
120 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-...
-2
votes
1answer
83 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
20 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?
-1
votes
2answers
63 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
40 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
28 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
120 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
41 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
60 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
48 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
63 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
132 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
361 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
3k 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 \...
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
85 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
155 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
73 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
58 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
63 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
78 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
63 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
43 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
55 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
100 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',...