All Questions
346
questions
2423
votes
12
answers
1.1m
views
How to replace a character by a newline in Vim
I'm trying to replace each , in the current file by a new line:
:%s/,/\n/g
But it inserts what looks like a ^@ instead of an actual newline. The file is not in DOS mode or anything.
What should I ...
549
votes
30
answers
94k
views
In Vim is there a way to delete without putting text in the register?
Using Vim I often want to replace a block of code with a block that I just yanked.
But when I delete the block of code that is to be replaced, that block itself goes into the register which erases ...
336
votes
10
answers
384k
views
How can I add a string to the end of each line in Vim?
I want to add * to the end of each line in Vim.
I tried the code unsuccessfully
:%s/\n/*\n/g
211
votes
6
answers
111k
views
How to replace a single word under cursor?
How do I replace a word under the cursor in Vim.
So instead of using dw then i then the word and then Esc, is there a simpler combination to replace the word under the cursor?
196
votes
9
answers
39k
views
Case preserving substitute in Vim
Can this can be done in Vim?
What I mean is: searching for 'BadJob'
and replacing with 'GoodJob' would do
the following replacements
'badjob' -> 'goodjob'
'BadJob' -> 'GoodJob'
'...
150
votes
6
answers
82k
views
How to search and replace globally, starting from the cursor position and wrapping around the end of file, in a single command invocation in Vim?
When I search with the / Normal-mode command:
/\vSEARCHTERM
Vim starts the search from the cursor position and continues downwards, wrapping around to the top. However, when I search and replace ...
146
votes
5
answers
105k
views
Search and replace on specific lines in Vim
I can use
:5,12s/foo/bar/g
to search for foo and replace it by bar between lines 5 and 12. How can I do that only in line 5 and 12 (and not in the lines in between)?
136
votes
13
answers
57k
views
Search and replace in Vim across all the project files
I'm looking for the best way to do search-and-replace (with confirmation) across all project files in Vim. By "project files" I mean files in the current directory, some of which do not have to be ...
122
votes
9
answers
53k
views
Capitalize first letter of each word in a selection using Vim
In Vim, I know we can use ~ to capitalize a single char (as mentioned in this question), but is there a way to capitalize the first letter of each word in a selection using Vim?
For example, if I ...
122
votes
11
answers
33k
views
Replace word with contents of paste buffer?
I need to do a bunch of word replacements in a file and want to do it with a vi command, not an EX command such as :%s///g.
I know that this is the typical way one replaces the word at the current ...
117
votes
6
answers
63k
views
Find and Replace within selection in `vi`
How do I do a Find and Replace within a selection in vi?
98
votes
2
answers
56k
views
vim does not find and replace simple phrase that is clearly present
I have a simple vim problem that Google hasn't managed to help me with. Any thoughts are appreciated.
I do the following search and replace:
:s/numnodes/numnodes1/g
On a file containing the ...
98
votes
1
answer
98k
views
How to include forward slash in vi search & replace
I have a file that contains the string usrbin. I want to search for usrbin and replace it with /usr/bin/.
I tried :%s/usrbin/usr/bin/g, but it's showing error E488: Trailing characters.
How do I ...
78
votes
7
answers
20k
views
Perform a non-regex search/replace in vim
When doing search/replace in vim, I almost never need to use regex, so it's a pain to constantly be escaping everything, Is there a way to make it default to not using regex or is there an alternative ...
75
votes
4
answers
52k
views
How to search and replace in current line only?
I see how to search and replace in specific lines, specifying by line number, and how to search and replace using the current line as reference to a number of lines down.
How do I search and replace ...
71
votes
6
answers
77k
views
/ in vi Search and replace?
in vi, search and replace, how do you escape a '/' (forward slash) so that it is correct. Say in a path.
like: /Users/tom/documents/pdfs/
:%s//Users/tom/documents/pdfs//<new text>/g --FAILS (...
70
votes
2
answers
11k
views
Substitute with contents of register or lines range from elsewhere in file in Vim
I'm using Vim, and I want to substitute some placeholder text with a long string, that spans several lines, which is already written somewhere else in the file.
Is it possible to replace a pattern ...
68
votes
3
answers
31k
views
Find and replace whole words in vim
To find and replace all instances of a word in vim, I use
%s/word/newword/g
How do I change this so that it only finds instances of "word" that are whole words?
60
votes
5
answers
20k
views
How to do search & replace with ack in vim?
I am using the Ack plugin in Vim, which helps me to quickly search for strings in my project. However, sometimes I want to replace all or some occurrences of the found strings. You can do some kind of ...
54
votes
4
answers
40k
views
Adding characters at the start and end of each line in a file
What is the best way to add some characters at the start and end of each line? Can it be done using Vim, or some other way?
52
votes
5
answers
23k
views
How to count search results in Vim?
How to find in the text file using Vim how many times specific word repeats.
For example, I want to see how many times word "name" repeats in the code:
"collection": "animals",
"fields": [
...
51
votes
6
answers
20k
views
Search & replace using quickfix list in Vim
So far I always used EasyGrep for replacing text in multiple files. Unfortunately it is quite slow when a project gets bigger. One thing that seems to be amazingly fast is Ggrep of fugitive.vim that ...
50
votes
2
answers
12k
views
Vim: Replace selection with default buffer without overwriting the buffer
Here is my problem:
I am in visual mode.
I select text and copy it to the buffer. ((y)ank)
I select another text which I want to replace and paste the buffer. ((p)aste)
Now the second selection ...
49
votes
3
answers
50k
views
Is it possible to use find and replace on a wildcard string in VIM?
For example, I have a bunch of values with a common prefix and postfix, such as:
fooVal1Bar;
fooVal2Bar;
fooVal3Bar;
In this case, all variable names begin and end with foo and end with Bar. I ...
43
votes
7
answers
19k
views
camelCase to underscore in vi(m)
If for some reason I want to selectively convert camelCase named things to being underscore separated in Vim, how could I go about doing so?
Currently I've found that I can do a search /s[a-z][A-Z] ...
40
votes
7
answers
21k
views
How can I delete all lines that do not begin with certain characters?
I need to figure out a regular expression to delete all lines that do not begin with either "+" or "-".
I want to print a paper copy of a large diff file, but it shows 5 or so lines before and after ...
37
votes
2
answers
12k
views
How can I replace a pattern only on lines that do or do not contain another pattern?
Say I have a text containing the words red and blue.
How do I replace occurences of the word blue with the word green only in all lines containing the word red?
Likewise how can I replace blue with ...
37
votes
4
answers
32k
views
How can I include the period (.) in Vim's search and replace command? (replacing .html extensions)?
When I do: /.html Vim looks for html
I would like to replace all the .html with .php
Something like this: :%s/html ext/php ext/g
35
votes
8
answers
173k
views
How to replace space with comma using sed?
I would like to replace the empty space between each and every field with comma delimiter.Could someone let me know how can I do this.I tried the below command but it doesn't work.thanks.
My command:
...
33
votes
9
answers
49k
views
Replacing quote marks around strings in Vim?
I have something akin to <Foobar Name='Hello There'/> and need to change the single quotation marks to double quotation marks. I tried :s/\'.*\'/\"\0\" but it ended up producing <Foobar Name="...
33
votes
4
answers
22k
views
Vim Search/Replace, meaning of %s
In Vim you can search/replace text in the following way.
:%s/old/new
What does the %s mean?
32
votes
3
answers
22k
views
Adding Line Break After pattern in VIM
I have a css file and I want to add an empty line after every }.
How can I do this in Vim?
31
votes
5
answers
36k
views
vim: How do I replace a character under my cursor with previously yanked text (more than once)?
Let's say I've yanked 3 characters "foo" into my clipboard by using a visual select + yank, ie: 'vllly'
Then I've moved my cursor to another character (let's call this character x) on line 5 which I'...
31
votes
3
answers
35k
views
How to search/replace special chars?
After a copy-paste from Wikipedia into Vim, I get this:
1 A
2
3 [+] Métier agricole<200e> – 44 P • 2 C
4 [×] Métier de l'ameublement<200e> – 10 P
5 [×] Métier de l'animation<...
28
votes
5
answers
21k
views
Search and Replace with incremented values in Vim
Lets say I wrote a simple CSS rule like this:
.star_10 {
background: url(stars.png) no-repeat 0 0;
}
And I need 10, so I copied it 9 times.
.star_10 {
background: url(stars.png) no-repeat 0 0;
}...
27
votes
3
answers
7k
views
vim search and replace limited to the highlight in visual block mode
I often have text in columns and need to replace some things without clobbering similar stuff on the same line... a simple example follows:
Suppose I have highlighted the text in grey with vim visual ...
25
votes
4
answers
14k
views
vim: replace all characters up to a given token
Using vim I would like to replace all characters up to a certain one with another character, say a blank space - without affecting the layout/number of characters in the line. Here's an example:
...
24
votes
1
answer
20k
views
How can I search for the dot character using the search command?
I'm trying to use the Search command in Vim:
:Rs/F/T/X
R = range
F = text to find
T = text to replace with
X = options
But, when I want to search for the "." (dot character) I'm getting some ...
23
votes
3
answers
2k
views
Convert vim / search to search and replace without retyping regular expression
When I perform a search and replace in Vim, I like to use the search function (/regex) first to visually test my regex.
Is there a simple way to bring up the expression I wrote in a search and ...
23
votes
2
answers
18k
views
VIM: how to replace a word
I guess, the problem is pretty common. Let's say, we have a source file and wish to replace some string with another string.
Usually, I use the following command:
:%s/test/testValue/gc
But, what if ...
22
votes
6
answers
9k
views
How to search and replace with a counter-based expression in Vim?
Is there a way to insert the value from some sort of counter variable in Vim :substitute command?
For instance, to convert this document:
<SomeElement Id="F" ... />
<SomeElement Id=&...
19
votes
3
answers
10k
views
How to search for a character the displays as "<85>" in Vim
I have a file that was converted from EBCDIC to ASCII. Where there used to be new lines there are now characters that show up as <85> (a symbol representing a single character, not the four ...
16
votes
12
answers
9k
views
What Vim command to use to delete all text after a certain character on every line of a file?
Scenario:
I have a text file that has pipe (as in the | character) delimited data.
Each field of data in the pipe delimited fields can be of variable length, so counting characters won't work (or ...
16
votes
4
answers
9k
views
How to replace ~ (tilde) in vim
I have a string with a ~ in it and using the expression
Example:
hi~how~are~you
:%s/~/ /g
This doesn't seem to work any ideas?
16
votes
4
answers
5k
views
Vim - Search and replace the results
I'm getting more and more comfortable with Vim after a few months.
BUT, there is only one simple feature I can't get any answer from the web. That is "Search and replace the results". The problem is ...
16
votes
5
answers
8k
views
Vim Search/replace: what do I need to escape?
I'm trying to search and replace $data['user'] for $data['sessionUser'].
However, no matter what search string I use, I always get a "pattern not found" as the result of it.
So, what would be the ...
15
votes
8
answers
38k
views
How to remove quotes surrounding the first two columns in Vim?
Say I have the following style of lines in a text file:
"12" "34" "some text "
"56" "78" "some more text"
.
.
.
etc.
I want to be able to remove the quotes surrounding the first two columns. ...
15
votes
7
answers
7k
views
In Vim how can I search and replace every other match?
Say I have the following file
<block>
<foo val="bar"/>
<foo val="bar"/>
</block>
<block>
<foo val="bar"/>
<foo val="bar"/>
</block>
...
15
votes
8
answers
8k
views
Deleting / changing searched text in Vim
When I do an interactive search for some pattern, each time I hit n, I get the next result. How do I delete / change each result that I come to?
Ideally, what I'm looking for would work like this: I ...
15
votes
5
answers
5k
views
Is it possible to disable Replace mode in vim?
Put simply, how can I completely disable Replace mode in vim? I never use Replace mode, but I sometimes end up in it by accident when re-entering Insert Mode. Naturally, I goof up by typing over a few ...