Finding and replacing Vim - limited to certain lines

I had a situation where I wanted to replace FOO with BAR through a file; however, I want to do this only in certain places, for example between lines 68-104, 500-537 and 1044-1195. In practice, I dropped the markers (ma, mb, mc, ...) on the lines of interest to you, and I ran the following:

:'a,'bs/FOO/BAR/g | 'c,'ds/FOO/BAR/g | 'e,'fs/FOO/BAR/g 

I had to repeat this several times with different searches and replace the terms s / CAT / DOG, etc., which made me sick to rewrite the command line each time. I was lucky that I had only 3 places in which I wanted to limit my search (imagine if it was 30 or 40, how messy the command line was).

Other than writing a function, is there an easier way to do this?

In the corresponding note. I copied FOO to 's' (search), and BAR to 'r' (replace) and tried the command line

 :'a,'bs/\=@s/\=@r/ | 'c,'ds/\=@s/\=@r/ | 'e,'fs/\=@s/\=@r/ 

It would save me to rewrite the command line every time, but, alas, it did not work. The replacement bit was fine \ = @r, but \ = @s in the search pattern gave an error.

Any advice would be appreciated.

+8
vim replace
source share
6 answers

If you need to perform a set of linear operations (e.g., lookups) in a bunch of different line ranges, one trick you can use is to make these lines different by first adding a prefix (this is not shared by any of the other lines).

Usually I do this to separate the whole file with something like >G executed on the first line, and then use the commands :s/^ /X/ or the block visual to replace the leading spaces with X on the lines I want.

Then use :g in combination with :s . eg:

 :%g/^X/s/FOO/BAR/g :%g/^X/s/BAZ/QUUX/g 

Finally, remove the temporary prefixes.

+5
source share

To get rid of the need to re-search the template, replace string and flags, you can simply use the command :& with the & flag:

 :'a,'bs/pat/str/g|'c,'d&&|'e,'f&& 
+4
source share

inspired by @Vdt answer:

I'm not sure, but you could write all the permutations to the file and source, which I think. substitutions.vim :

 68,104s/FOO/BAR/g 168,204s/FOO/BAR/g 618,644s/FOO/BAR/g 681,1014s/FOO/BAR/g . . . 68,104s/BAZ/BOOO/g 168,204s/BAZ/BOOO/g 

and then :so substitutions.vim , maybe you can also use this for multiple files of the same structure. you can add e to add an ignore error message if it is not clear that the substitutions are found in the corresponding lines of the line.

+3
source share

Instead of using a marker, use this:

:68,104s/FOO/BAR/g << substitue from line 68 to 104

This should make your work a little easier and more understandable.

+2
source share

With q: you can recall the previous lines of commands and edit them like a normal Vim buffer so that you can quickly replace FOO and BAR with something else, and then re-execute the line with Enter .

s/\=@s/\=@r/ does not work; as you said, this only works in the spare part. But for the template, you can use Ctrl + R Ctrl + R s to insert the contents of register s instead of \=@s . It is preferable to use the default case, then it is a simple s// , but you probably already know that.

+2
source share

When performed on a closed fold, replacements are limited to this fold.

  • collapse each area
  • hover over one closed fold
  • perform the substitution:: :s/foo/bar<CR>
  • go to the next closed hide zj or zk
  • use command line history :<Cp><CR> or :<Up><CR> to perform the same lookup
  • repeat ...

You can also add the c flag at the end of your wildcard so that Vim asks you to confirm it before it is actually executed. This can be tiring if you have many matches.

+2
source share

All Articles