Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [replace]

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

1
vote
1answer
22 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
42 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
29 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 ...
1
vote
2answers
45 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 ...
0
votes
1answer
41 views

Search and replace a pattern with new line except for a pattern

I am having a very long line file, I have to break the line into number of small lines. Example: < this>is a testing< /this>< next>testing< /next>< tested>successfully&...
1
vote
0answers
41 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
26 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
62 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, ...
0
votes
1answer
33 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 ...
3
votes
4answers
83 views

How to run a python command based on a matched group of a pattern?

Let say I want to run a command that uses a matched group of a pattern for each line matching the pattern. The natural choice is :g. With the command :s, there is no problem. If i would do :g/aaa\(....
0
votes
2answers
51 views

how to replace N characters without having to press <esc>, is it possible?

In command mode, r replaces the character under the cursor by the one pressed right after typing r without going into insert mode, so there's no esc to press. 5r replaces the 5 characters starting ...
2
votes
2answers
166 views

Using global command on Quickfix entries [duplicate]

I find and replace a pattern in all the files in my project (which is a big one). After the search is done, quickfix window is opened with the matches. The matches are in different files in different ...
2
votes
2answers
345 views

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 ...
0
votes
1answer
15 views

How to automatically return to where the cursor was before executing cdo?

Is it possible to automatically return to where the cursor was before running :cdo command? Currently, the cursor ends in the last file in the quickfix, then I have to navigate back to where I was ...
1
vote
2answers
69 views

Change Case of last searched pattern

I often use # key to quickly search for a keyword. I want to change the case of the last searched pattern in the easiest possible way. One way to do this is to use \U\1 in the substitution part and ...
0
votes
1answer
44 views

Replace a line with two other lines

I have multiple files with a string like: source /PATH/TO/OLD/ENV.sh And this needs to be replaced with: source /PATH/TO/NEW/ENV1.sh source /PATH/TO/NEW/ENV2.sh Usually, I use sed to replace ...
0
votes
3answers
79 views

How to suppress the error {Error detected while processing BufRead Auto commands for “*”}

I want to alter existing files automatically,to replace all tab with four blanks in existing file. vim .gvimrc autocmd BufRead * exec "%s/\t/ /g" The error info pop up when to open a file ...
3
votes
1answer
95 views

How do I start overwriting visually selected text without c

I would like to start overwriting immediately visually selected text without any additional keystrokes such as c. I am looking something similar to normal text editor where you can straightaway ...
0
votes
1answer
33 views

mapping to enclose symbol under cursor with an expression that contains the symbol (revisited)

@saginaw answered a question I asked a long time ago, in this thread. Here's a code snippit of his suggestions that illustrates my problem: function! s:PadMacro(type) abort if a:type ==# 'char' ...
2
votes
1answer
54 views

Convert a search and replace to a function

Given this search and replace expression, which works fine on one or multiple visually selected lines: :'<,'>s/\v.{-}(`.{-}`).{-}(\\'.{-}\\').*/$this->execSQL('DELETE FROM \1 WHERE id = \2;')...
0
votes
2answers
45 views

vim: replace n strings with another strings

After reading https://stackoverflow.com/questions/7338794/vim-regex-replace-with-n-characters, I've known how to replace tabs by spaces: :%s/^\v(\t)*/\=repeat(repeat(' ',4),strlen(submatch(0)))/g ...
0
votes
1answer
54 views

Replace capitalized letters in matching lines

I have multiple lines with the key "booktitle": booktitle={Advances in Cryptology - CRYPTO' 89 Proceedings}, volume={435}, series={Lecture Notes in Computer Science}, I would like to replace ...
1
vote
1answer
244 views

Find the nth word in a line, and replace it back into the line in different position

I have searched unsuccessfully everywhere for an answer to this question and could not find an acceptable answer. I am taking a list of table names from a database, and dynamically creating alter ...
0
votes
2answers
51 views

limiting global search&replace to matches that occur immediately to the right of the cursor

If I have this line: a{word} a{20] a{word} a{20] and type :g/a{.\{-}\]/s/{/\[/gc vi prompts me to change all four occurrences of { to [. I want it to prompt me to replace only the matches ...
0
votes
1answer
101 views

Disable mapping delay

I saw this wonderful video and really love the config of it, which is why I wanted to replicate it. The following line implements a double space keymapping which searches for the next <++>, ...
0
votes
0answers
53 views

Visual selection and replacement in multiple lines to perform DIFFERENT edits in vim

How can I select different portions of multiple non-contiguous lines and replace them with the different text? Example: Let's say my buffer looks like this: Roses are reed, Violets aree blue, yellow ...
2
votes
2answers
44 views

How to search and replace in the history

Is there a way edit command line or search history? I have read Is there search and replace history in vim? and tried using q: to open the history and do changes there, but all it seems to be able to ...
1
vote
1answer
34 views

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 ...
1
vote
1answer
63 views

how to write following pattern in vim?

I am sorry for the rather fuzzy title of my question. I am working on some text, which I take from web-page and try to annotate it with LaTeX commands using Vim. How can I write in Vim following ...
-1
votes
1answer
72 views

Is \r operating system specific?

In the Vim documentation, it says that \r "matches < CR >", however for the result of replacement I could not find in the documentation a description of what it does. For example, imagine this: :%...
3
votes
1answer
214 views

How to put space before and after operators for enhanced readability?

I want - variable++; then variable ++; variable1 + variable2 nothing to do variable1 +variable2 then variable1 + variable2 variable1+ variable2 then variable1 + variable2 variable1+variable2 then ...
0
votes
1answer
32 views

Matching delimited text, taking into account level

I am looking for a way to replace \cmd1{stuff \cmd2{other stuff}} with (stuff \cmd2{other stuff}) I have tried :%s/\\cmd1{\(.\{-}\)}/(\1)/gc However, this returns (stuff \cmd2{other stuff)} ...
1
vote
2answers
79 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
1answer
286 views

Finding all semicolons not preceded by backslash

I'm really struggling to figure out how to find all occurrences of a semicolon not preceded by a backslash. I'm hoping to find a quick vim command to clean up long bash commands that I've opened with ...
3
votes
2answers
73 views

search nongreedily for a bunch of terms delimited by parentheses

I have something like the following text ( parens ) ( 1 + growth ) (1+growth) (1 +growth) ( parens ) and I'd like replace every instance of ( 1 + growth ), regardless of whitespace, with say, ...
0
votes
1answer
33 views

What is the syntax if I want to replace one directory with another in vim given that I have '/' in it?

If I have this directory: /home/abc/def/apples/ and I want to replace it with this directory: /home/nfl/nfl/nba/ in vim, what is the syntax? I tried using %s/a/b/g but this command fails.
0
votes
1answer
80 views

Vim - find an occurence of a pattern one by one and and edit it at once

I would like to be able to find an occurence of a pattern, highlight it, go to the next occurence of the pattern, highlight it, go to the next one, and so on and so forth, and on the end delete all ...
2
votes
1answer
141 views

Sub replace if whole line match pattern

How to replace only if whole line match pattern? Example, input: foo, bar, baz # lorem, ipsum, ret Example, output: foo, bar, baz # lorem, ipsum, ret Should only replace commas with comma+...
6
votes
3answers
593 views

Delete block of code if a pattern is (not) detected in one of the lines

I have managed to do this with a super long find-replace but I'm sure there's a simpler way: I have a file of the form: E01 2017 10 24 00 00 00-1.90001e-04-8.79280e-12 0.00000e+00 3....
1
vote
1answer
32 views

Can I remap <C-(key)> while replacing a character? If not, is there a workaround or hack?

I have i-mapped <C-a>, <C-s> etc. to commonly used keys that I use often but find annoying to type normally, for example * and _. It works as expected in insert mode, but when executing a ...
2
votes
2answers
55 views

How do I replace a word without putting the word into a register?

Right now I press ce to replace a word. However, this overrides anything that was in my register before. How can I replace a word and go into insert mode after without replacing what was in my ...
1
vote
2answers
1k views

vim how to move last word of each line to beginning of line using regex and backreferences?

I've been trying to select all characters except the last word (2 groups where 1 group is everything but the last word and the other group is only the last word) :%s/^.\+\([^\w\+]$\)/\2\1/g is what ...
1
vote
1answer
26 views

How to search and replace each hit with a multiline register in which each hit is replaced with an individual line from the register?

so if i have the following multiline text in my register +: ZERO SIGN CARRY AUXILIARY PARITY OVERFLOW INTERRUPT DIRECTION TRAP And I have the following code: case ZERO : ...
1
vote
1answer
322 views

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?
1
vote
1answer
36 views

replacing character in a visual block selection replaces the wrong character?

In visual selection mode, accessed with ctrl-v, I can replace the character I want (say Z to be replaced by A) within my selection by typing : followed by s/Z/A. This is extremely useful. Here is a ...
1
vote
2answers
53 views

How do I replace only the current instance of the search pattern without losing the pattern?

I often find myself doing an interactive search and replace for a string that may be either a whole word or a partial word, such as changing bar to jaz and also foo_bar_blah to foo_jaz_blah. So I can'...
0
votes
1answer
114 views

Replace brackets and the content within the brackets in a variable

I'm currently stuck on this problem. I want, in a vimscript, to replace a variable's brackets when they contain a certain pattern. To say this differently, say I have a variable foo = "[content] bar" ...
1
vote
2answers
58 views

Replace a word in a stream of words separated with underscore

Does anyone know a quick shortcut I can use to replace a word in a series of words connected with underscores? Given: Create_in_one_of_these_words I want to make, where | is cursor: Create_in_|...
1
vote
2answers
207 views

Replace two word sequence with second word

I need to replace on all lines sequence of words: public boolean public string public IList etc.. with modifier removed aka: boolean string IList Is there a way how to remove first part of match ...
4
votes
2answers
202 views

How to add accent to letters in normal mode?

I want to edit words that need accents (á) were they are missing. It's no problem to write down accents in insert mode: it is just "' + letter", so I can easily write á, é, í, ó, ú. How do get ...