All Questions

Tagged with
Filter by
Sorted by
Tagged with
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 ...
Vinko Vrsalovic's user avatar
  • 336k
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 ...
Edward Tanguay's user avatar
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
Léo Léopold Hertz 준영's user avatar
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?
sayth's user avatar
  • 6,846
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' '...
davetapley's user avatar
  • 17.5k
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 ...
basteln's user avatar
  • 2,580
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)?
pfnuesel's user avatar
  • 14.7k
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 ...
psyho's user avatar
  • 7,152
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 ...
keelar's user avatar
  • 5,960
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 ...
plong's user avatar
  • 1,733
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?
Agnel Kurian's user avatar
  • 58.7k
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 ...
Mark C.'s user avatar
  • 1,875
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 ...
user1578447's user avatar
  • 1,081
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 ...
Mike Crittenden's user avatar
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 ...
mherzl's user avatar
  • 5,874
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 (...
JT.'s user avatar
  • 905
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 ...
Paolo Tedesco's user avatar
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?
neuromancer's user avatar
  • 54.7k
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 ...
Ralph von der Heyden's user avatar
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?
Myth's user avatar
  • 1,582
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": [ ...
Artur's user avatar
  • 741
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 ...
medihack's user avatar
  • 16.3k
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 ...
Juergen Riemer's user avatar
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 ...
dwcanillas's user avatar
  • 3,601
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] ...
Jeff's user avatar
  • 1,837
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 ...
mager's user avatar
  • 4,835
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 ...
Kristian's user avatar
  • 1,727
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
wyc's user avatar
  • 54.4k
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: ...
Teja's user avatar
  • 13.4k
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="...
ravuya's user avatar
  • 8,686
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?
9er's user avatar
  • 1,674
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?
Chalist's user avatar
  • 3,220
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'...
Kosta Kontos's user avatar
  • 4,232
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<...
Olivier Pons's user avatar
  • 15.6k
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; }...
Fred Fickleberry III's user avatar
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 ...
Mike Pennington's user avatar
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: ...
canavanin's user avatar
  • 2,589
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 ...
Lucas Gabriel Sánchez's user avatar
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 ...
Robert S Ciaccio's user avatar
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 ...
Stas's user avatar
  • 11.7k
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=&...
Merlyn Morgan-Graham's user avatar
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 ...
Thomas G Henry LLC's user avatar
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 ...
Jason Down's user avatar
  • 21.9k
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?
Jeremy E's user avatar
  • 3,704
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 ...
Khiet's user avatar
  • 902
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 ...
Falassion's user avatar
  • 731
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. ...
Jason Down's user avatar
  • 21.9k
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> ...
Osada Lakmal's user avatar
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 ...
Chetan's user avatar
  • 47.3k
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 ...
coderintherye's user avatar

1
2 3 4 5
7