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
1answer
112 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
790 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
54 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 ...
1
vote
1answer
640 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 <++>, ...
2
votes
2answers
192 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
43 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
91 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
102 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
767 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
37 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)} ...
0
votes
1answer
3k views

E488: Trailing characters error in VI with find and replace command

I am trying a simple find and replace function in VI Editor to change a profile path but the / is getting in my way and I need a simple and clever solution. I tried using: %s/profile: "/fldr0/local/...
6
votes
2answers
3k 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
1k 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 ...
2
votes
2answers
82 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
58 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
130 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
603 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+...
5
votes
3answers
921 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
47 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
70 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
2k views

Find and replace string with slash in it and retain the slash in the new string

I am having a tough time finding and replacing a string using vi. Source String: &DB2_PORT;/&DB2_DATA; New String after replace: &DB2_PORT;/&DB2_DATA;:tcp=true I can find &...
2
votes
2answers
4k 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
37 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 : ...
2
votes
1answer
2k 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
58 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 ...
2
votes
2answers
154 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
606 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
347 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
735 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
1k 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 ...
0
votes
1answer
586 views

How to escape @ in search replacement

I usually have to worry about escaping search parameters to get special characters to match literally, but this time I'm seeing a problem with my replacement string. I want to take text like this: @@...
1
vote
3answers
141 views

Copy a line, comment it and replace a word in copied line

Libraries evolve all the time and I often face the situation where after an update some function name have changed and they return errors in the updated version of the library. It's easy to replace ...
0
votes
1answer
49 views

How to use regular expressions with \w\d inside ()

I created an example of text: dog cat worm And I want to replace everything but not the first word and without knowing what's there. At first I tried this::%s/\(dog\)\(.*\)/\1/g This works fine ...
0
votes
1answer
95 views

Recognizing expression written in assembly containing $ and new line

I'm trying to remove instances of the following: add $t1, $zero, 9 sw $t1, 8($t4) Where 9 could be any number, however 8 is consistent. If I copy this expression in Vim I get: ^Iadd $t1, $zero, 9^M^...
2
votes
1answer
286 views

Replace surrounding quotes for < >

Given the string "hello.h", I want to obtain <hello.h>. I have tried vim-surround, but while it seems to be able to surround with matching { }: cs"{ ( ): cs"( [ ]: cs"{ XML tags: cs"<mytag&...
3
votes
1answer
328 views

Character in math mode replacement

I would like to replace instances of a single character variable, say x, throughout all my equations in a latex file, say the replacement is \x. The equations are denoted by one of the following: \...
1
vote
1answer
83 views

How to repeat a certain operation across lines of code? [duplicate]

I need to repeat a certain operation across several lines of code spread through out the code base. For instance, x = y; needs to be replaced with EQUATE(x,y); However each instance needs to be ...
5
votes
1answer
182 views

Why copying from named register cannot be repeated using dot operator?

I am copying a word with: viwy (visual, inner, word, yank) Now I would like to replace some other word: viwp. This works only once, because each word is replaced by the last removed word. Ok, so I ...
0
votes
1answer
314 views

. (dot) command and backspace

Say I want to turn this: find(vec_a.begin(), vec_a.end(), c) into this: find(vec_b->begin(), vec_b->end(), c) That's what I just wanted to do, so I tried this (commands separated by commas): ...
4
votes
4answers
1k views

Replace text in visual mode in vim/vi

Here's a snippet that I'm working with (* denotes cursor location) *"First random string" ,"Random string I want to remove" } "Random string number 2" , "Another random string I want to ...
0
votes
1answer
117 views

How do you sort lines by longest substring to shortest substring?

Here are some sample strings, sorted by longest string to shortest sting: "Bi(NO<sub>3</sub>)<sub>3</sub>·5H<sub>2</sub>O","bismuth nitrate pentahydrate"); "Be(C&...
1
vote
1answer
178 views

Replace n with n+1

I want to replace a number n, with the number n+1. Here's what's working: :%s/\([0-9]\+\)/\=(submatch(0)+1)/ But this one doesn't work: :%s/-c \([0-9]\+\)/-c \=(submatch(0)+1)/ Why?
1
vote
0answers
33 views

substitute a join for a character that ends a line

when pasting from a pdf document, words in the original document are being split up, and replaced by a - and a line break. For example: alternative specifications. In con- clusion, these findings ...
1
vote
1answer
268 views

Pass word one-by-one and replace (Search and replace with confirmation) [duplicate]

I'd like to pass through a text an replace word1 by word2. However, I don't want to execute this task in one pass, but rather word by word. I know I could put the cursor on the word I want, execute *, ...
1
vote
1answer
1k views

Replace numbers with white spaces

I would like to replace all floating point numbers within given block with white spaces so that the number of the latter corresponds to the "length" of the floating point number. For instance, I want ...
0
votes
1answer
57 views

Modify beginning of the line based on pattern at end of line

In file whenever I am coming across (int or string) id","type": "int"}, name","type":"string"}, I want to replace with private int id private String name Need VI command which updates private ...
0
votes
2answers
103 views

Search and replace workflow [closed]

Classical replace command looks like %s/old_pattern/new_pattern which is fine, but working with code or constructing complex search regular expression is not the best option. I'd like to present my ...
2
votes
2answers
62 views

Why can't add a character at the beginning of each line in the edited article?

:set lcs=precedes:^,eol:# I want to insert a character ^ at the beginning of each line. Here is the result of :set lcs=precedes:^,eol:#. Why lcs=precedes:^ can't add a character ^ in the article ...
3
votes
1answer
578 views

vim search for arbitrary combination of spaces and tabs

I'd like to construct a mapping that would replace a b with c where the gap between a and b could be zero or more spaces or zero or more tabs, but not zero of both. There have been several ...
1
vote
1answer
109 views

Finding all occurences of backslash and deleting twice before every occurence of them

I got some output code that has random backslashes, followed by a new line. For example: ( 1-z+z^11-z^12+z^22-z^23+z^33-z^34+z^37-z^38+z^40-z^41+z^43- z^45+2*z^46-2*z^47+z^49-z^50+z^52-z^53+2*z^55-2*...