Stack Exchange Network

Stack Exchange network consists of 175 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
17 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
173 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
42 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
26 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
38 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
28 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
30 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
39 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
148 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
45 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
45 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
38 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
49 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
44 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
58 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
42 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
29 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
71 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
74 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
118 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
53 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
239 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
416 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
16 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
70 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
45 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
106 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
166 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
37 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
73 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
51 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
57 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
384 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
222 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
56 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
51 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
36 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
65 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
80 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
283 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
33 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
128 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
496 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
76 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
35 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
91 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
179 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
698 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
33 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 ...