Questions tagged [replace]

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

263 questions
12
votes
2 answers

Replace current selection and all identical occurrences in a file

This is a sample sentence. This is a second sample sentence. Let's say I select 'is a' of the first sentence. Now I want to change all occurrences of what is selected (including the selection itself) to 'is not a'. How do I do that?
Rotareti
  • 605
  • 5
  • 16
5
votes
3 answers

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
duff18
  • 195
  • 1
  • 1
  • 6
4
votes
3 answers

Concatenating continuation lines

I have a text file like this: Line text,line text next line 3rd line Line x where 2nd and 3rd, etc lines begin with a single space. I need to concatenate all lines prefixed with a space to the previous one without a space, so that the result…
4
votes
4 answers

Plain search and replace like Notepad?

I am aware of the substitute command and of its usage as an ex command :%s///g However when contains a very complicated string, say a complicated URL with lots of characters like % [ ] : / \ etc, then the characters start being part…
user3203476
  • 399
  • 2
  • 8
3
votes
1 answer

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 line using a simple and elegant one liner. Is…
Segfault
  • 33
  • 3
3
votes
1 answer

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 cursor is over i, a, A to jump into insert mode ce,…
danielson317
  • 133
  • 4
3
votes
1 answer

Replacing string including a metacharacter

I am learning to use VIM to write Python code. Frequently, I need to perform replacements that include metacharacters, e.g. replacing all instances of '2**NN' with 'MM'. How can I achieve this?
Stefano
  • 33
  • 3
2
votes
4 answers

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,word3, word4" became (note the double white space) "word1, word2, word3, …
elbarna
  • 179
  • 4
2
votes
3 answers

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"] with my_params.some_property I have some difficulty replacing…
Lionel Hamayon
  • 233
  • 2
  • 8
2
votes
1 answer

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…
cyberixae
  • 121
  • 1
2
votes
3 answers

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', 'css/styles-l', …
2
votes
2 answers

Add prefix to numbers at beginning of each line

Should be straight-foward but cannot get it working. I have a text file that has numbers at the beginning of some lines. For e.g: 1. Some text.... running to many lines 2. Some more text ... 3. A long line... that spans... many more.. lines I'd…
linuxfan
  • 123
  • 5
2
votes
1 answer

Replace same match with squential list

I find myself more then once doing this repetitive thing: I have something like: do amount_foo blah_blah I need to copy this n times, but change the foo part. I copy and paste it as many times as needed: do amount_foo blah_blah do amount_foo…
user3342816
  • 300
  • 1
  • 9
1
vote
0 answers

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(flag1); delete flag1; I've tried replace every…
Mactarvish
  • 11
  • 1
1
vote
1 answer

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. How do I use regex/replace on them? For example I…
xxjjnn
  • 123
  • 5
1
2 3