Questions tagged [replace]

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

Filter by
Sorted by
Tagged with
28
votes
5answers
1k views

How to replace a word with a yanked word in normal mode

When I have to replace a word with another word I yank before, I usually use a workflow which seems unnatural to me: Let's say I have this text: foo bar buzz And I want to yank foo and replace bar ...
21
votes
2answers
11k views

Replace a pattern with current line number

I'd like to replace 'x' in this file with current line number. That is, change x x x to 1 2 3 For now, I use a Perl one-liner: perl -pi -e '$x=$.; s/x/$x/' myfile.txt I think UltraEdit can do it ...
15
votes
2answers
10k views

How to partially replace text in a selected text-block?

Not easy to find a nice title for my problem. I'm aware of inserting in front of a text-block: Ctrl-v select lines I type text ESC Now I would like to do this but also with replacing a part in my ...
14
votes
3answers
7k views

How to replace more than one character without entering insert mode

I have a file with a line that looks like this: foofoo.xy.barbar I want to change the xy to gh. I tried to do 2r -> gh, but obviously that didn't work, as it replaced the xy with gg and then moved ...
12
votes
3answers
431 views

How to selectively change a hightlighted string?

By searching, I've highlighted the string that I want to change. Now I want to replace only the highlighted text. I'm using n to advance to the next instance of the search string. I was thinking I ...
12
votes
3answers
11k views

Insert an incrementing number to each line in either a selection or a match

I have a problem I can think of two general approaches for solving, but I don't know specifics for either approach. ... Level 1: cũng also Level 1: và and Level 1: như like; such ...
12
votes
3answers
24k views

How to add space on multiple lines when pressing spacebar (without extra configuration)?

I tried visual block, but visual block only allow to highlight or delete characters on multiple lines. Is there any function that allows to add space before the first character of multiple lines when ...
11
votes
10answers
1k views

Simultaneous find and replace

Is there a way to perform a simultaneous find and replace? e.g.: I want to replace all 1 for 0 and simultaneously all 0 to 1 [ [1, 1, 1, 1, 1, 0], [1, 1, 1, 1, 0, 1], [1, 1, 1, 0, 1, 1], ...
10
votes
6answers
6k views

How to copy first four characters of every line to the end of that same line?

Given a series of lines that look similar to this: 2001 "Some Kind of Title," Author's Name, Publication Name, 1 Mar. 2002 "Some Kind of Title," Author's Name, Publication Name, 12 Oct. 2003 "Some ...
10
votes
3answers
306 views

Alternative to escape slashes in search and replace for long strings

Is there any alternative in vim search and replace for strings, than having to escape every special character in string. For example, If I want to match following string and replace with something [...
10
votes
5answers
3k views

Replace selection with output of external command

Assuming I have a line in a file with XXXX being a placeholder I have selected via v as follows: XXXX Some other text How can I change only the XXXX with the output of an external command, such as ...
10
votes
3answers
536 views

Is there a motion similar to a" that never includes leading whitespace?

I'm using the a" motion rather often in vim; e.g. using ca" while refactoring a piece of code to replace a hardcoded argument with a variable name. The problem with this approach is that a" includes ...
10
votes
2answers
7k views

Multiline Search and Replace

Would like to perform a search and replace in a file with 12000 lines. Specifically, if an occurrence of ^ SetFontSize 28 exists after a ^Hide block and before the next ^Hide or ^Show, change 28 ...
10
votes
2answers
493 views

Most efficient way to replace the entire buffer?

Currently, I have two similar methods to replace the entire buffer with some other text (say in clipboard). Method #1: delete all lines: :%d. paste text: "+P ( or similar: :put+). Method #2: ...
9
votes
7answers
4k views

How to replace a pair of brackets

I have a file, containing some lines like this: aaa bbb (ccc) ddd. Now I want to replace the pair of brackets by a pair of square brackets to make this line become aaa bbb [ccc] ddd. I think that \...
9
votes
2answers
6k views

Nested matching group in regex

I have a common usecase when I transform some python expression the following way: value 1 value 2 value 3 into ['value 1', 'value 2', 'value 3'] The easiest way may be to use a mapping, but I ...
9
votes
1answer
1k views

How to get a preview for the substitution command?

Quite often I find myself in need of performing a substitution that will have matches sparsed all over a large file and with non-trivial search and replacement expressions. For example, say I have ...
8
votes
4answers
270 views

Use pattern of global ex command found on a line to substitute in another line

In my fortran code I have a lot of blocks like the following subroutine name(arg1,arg2,arg3,...) : end subroutine and I want them to become subroutine name(arg1,arg2,arg3,...) : end subroutine name ...
8
votes
1answer
234 views

Perform previous substitution for different selection with single keystroke

I performed following command :'<,'>s/2/digit/g on the code below manually visually selected: return _.round(this.getTotalAbs(), 2).toFixed(2); And it worked as intended return _.round(...
8
votes
2answers
2k 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 ...
8
votes
2answers
929 views

How do I paste the same line of code X times but change one word in the line each time?

I'm writing HTML lists, and I want to be able to automagically paste 10 list items where the item text is the only thing that changes. I could accomplish this using PHP and then just copying the ...
8
votes
1answer
129 views

How can I use consecutive numbers in an Ex-style substitute command?

For example, let's say I want to put a number before every line that starts with the word "Do". The command would look something like :%s/^(Do )/1. \1/, but what can I do so it will apply numbers ...
7
votes
3answers
163 views

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

Have been reading vim scripting posts for an hour, but I'm afraid I don't have the skills to apply these posts to my problem. Editing math in latex documents, I'm frequently wanting to replace math ...
7
votes
2answers
3k views

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) ...
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/.
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....
5
votes
1answer
249 views

Replace ONE occurrence of something with MULTIPLE instances of something

How can you replace one match with multiple instances of a character (like \t)? It's no problem to replace multiple characters with one. But I did not find the answer for the opposite. For example: ...
5
votes
2answers
154 views

How to replace buffer with a search range selection?

I'd like to replace the entire buffer (file) with the current search range selection: :/pattern1/;,/pattern2/p How this can be achieved?
5
votes
3answers
670 views

Why does replace not accept a motion?

A lot of operator like delete, change, indent, switch-case accept a motion to apply on. For example, let say I want to replace the current word with all letter a, I do: viwra Compared to the ...
5
votes
1answer
499 views

Repeat replace one character (r) for next occurence

I'm using r a lot (e.g. for replacing " with ' when refactoring code). Is there an easy way to do something like this: For text: values = ["a", "b", "c"]. Place cursor on first ", press r' to change ...
5
votes
3answers
549 views

How can I replace a word in a column without changing the width

I'm writing cucumber example tables, which are formatted like this: | title | title | | data | data | | data | data | How can I replace one of the data or title cells with a word of ...
5
votes
1answer
196 views

In Vim, how can you delete following identical rows in a visual selection, without sorting?

So Lets say I have a list \Summit \SummitSign \sun \ProgrammingOnABoat \SunCloud \SunshineOpenCircled \sup \supdsub \supdsub \supedot \suphsol \suphsub \Summit \SummitSign \sun \Summit \...
5
votes
1answer
2k views

How can I replace a word with a yanked word in Vi?

Lately I've found myself working more and more with remote servers over SSH. Most of those severs don't have Vim, but just Vi. Using it hasn't been too rough with just a few problems. One is this: ...
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 ...
5
votes
1answer
91 views

Vim find/replace highlighting

I'm a long time Vim user but a few months back started dabbling in Emacs. One of the things I really liked about Emacs was when doing a find/replace s/you/me it would highlight the word I was ...
4
votes
3answers
907 views

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 ...
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 ...
4
votes
2answers
4k views

Searching/replacing exact word in vim by default

In vim when searching or replacing an exact word you need to type: \<word\> to match it exactly, this is cumbersome to type, and I find myself wanting to search/replace exact words a lot more ...
4
votes
1answer
2k views

Change/delete matching brackets at the same time

Sometimes I write something in brackets and then realize I've typed the wrong kind. For instance, in LaTeX: \command(arguments) Which should be: \command{arguments} I can jump between the two ...
4
votes
2answers
105 views

How to change text in other line when the pattern is found?

I've got this 4-line content: foo baa baz quz and I'd like to replace baa to bar only when quz is found. I've tried these commands (which highlights baa correctly, but says it's not found): :/quz/...
4
votes
1answer
111 views

Search and replace depending on capitalization of letters [duplicate]

I would like to replace foo with bar and Foo with Bar. I can do :s/Foo/Bar/g :s/foo/bar/g Can I do this in one command?
4
votes
1answer
128 views

Difference between two end of line signs

Both $ and \n represents the end of a line. To remove an empty line we can use either :g/^$/d or :%s/^\n//g My questions are: What's the difference between these two signs? It seems the first ...
4
votes
1answer
1k views

Change '\r' to '\n' for search and replace?

In expression :s/foo/bar\n/g When replacing \n inserts a null byte, according to the docs I have to use \r. Since I insert newlines more frequently than null bytes, is there a way to change this ...
4
votes
1answer
115 views

search & replace: how to add a line feed to break up a line?

So, I have the following line where I would like to find all the {#}= and put them on a new line. Of course there can be more then 9 value entries and there is more text on the line, so I am thinking ...
4
votes
1answer
296 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 ,...
4
votes
1answer
123 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 ...
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 ...
4
votes
4answers
131 views

Plain search and replace like Notepad?

I am aware of the substitute command and of its usage as an ex command :%s/<old>/<new>/g However when <new> contains a very complicated string, say a complicated URL with lots of ...
3
votes
3answers
1k views

Select multiple words, one at a time, then replace them all

Goal: Select multiple words, one at a time, then replace them all. I'm new to Vim; I come from Visual Studio. I would like to achieve one important feature that helped me a lot in my productivity: ...
3
votes
2answers
1k 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 ...

1
2 3 4 5