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
2answers
31 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
129 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
37 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
42 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
27 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
46 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
35 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
50 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
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
69 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
57 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
94 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
195 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
388 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
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
91 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
132 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
36 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
60 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
48 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
56 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
312 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
152 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
54 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
47 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
35 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
64 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
76 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
246 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
93 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
381 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
74 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
86 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
161 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
627 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
27 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
424 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
54 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
127 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" ...