5

I have a file with strings:

 8 deb http://ftp.de.debian.org/debian/ stable main contrib non-free
 9 deb http://ftp.de.debian.org/debian/ stable main contrib non-free
10 deb http://ftp.de.debian.org/debian/ testing main contrib non-free
11 deb http://ftp.de.debian.org/debian/ testing main contrib non-free
12 deb http://ftp.de.debian.org/debian/ sid main contrib non-free
13 deb http://ftp.de.debian.org/debian/ sid main contrib non-free
14 deb http://ftp.de.debian.org/debian/ experimental main contrib non-free
15 deb http://ftp.de.debian.org/debian/ experimental main contrib non-free

I need edit address only on lines 9,11,13,15. I'm just curious, is there any simple hack (like select only this lines and substitute in selected range) in vim to do that? Or should I record a macros and apply it to strings I need.

rush
  • 2,484
  • 2
  • 19
  • 31
  • the 8,9,10.... are also in text or just line number to help to explain your question? – Kent May 08 '13 at 11:39
  • nope, they're just a line numbers to explain. – rush May 08 '13 at 11:58
  • I am curios to know whether it is possible. In the meanwhile, maybe you can use `:%s/foo/bar/gc`. It changes each 'foo' to 'bar', but ask for confirmation first. – ThanksForAllTheFish May 08 '13 at 12:10
  • 1
    Another option would be to substitute in one line, and then use the `&` command to repeat the substitution in the other lines. – Carlos Campderrós May 08 '13 at 12:14
  • possible duplicate of [Find and replace strings in vim on multiple lines](http://stackoverflow.com/questions/19994922/find-and-replace-strings-in-vim-on-multiple-lines) – Anshul Goyal Jun 29 '15 at 15:58

2 Answers2

3

If there is possibility to select those lines by regex, we could do

:g/pattern/s/foo/bar/

If it is not possible, you need to write a small function. The function is not complicated, just getline (with lineNO in your given list), then do substitution, finally setline back. However I don't know if this belongs to your "simple hack".

With function, you could also do like this:

do substitution on lines with  10 <line number <50 and line number is odd.

so that you don't have to type those numbers.

You may realize that, shell command is easier to handle this kind of stuff. You have option to call external command to process your text in vim.

for example:

%!awk 'NR==3||NR==5{gsub(/deb/,"foo")}1'

or do the substitution with above example, odd line number between: 10-50

%!awk 'BEGIN{for(i=11;i<50;i+=2)l[i]}NR in l{gsub(/foo/,"bar")}1'

also you could visual select text and pass them to external command.

hope it helps.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • Thanks for help. In that way we can use sed, that looks more comfortable for that particular task. `sed '9,15{s/foo/bar/;n;}'`. I was curious about internal things ( or plugins ). – rush May 08 '13 at 13:54
  • well awk is just an example of external command. personally I feel awk is more comfortable than sed. :) – Kent May 08 '13 at 14:45
  • I think Vim users in general will feel more comfortable with sed, since both sed and Vim are based on ed and therefore have similar syntax. – Idan Arye May 08 '13 at 18:17
1

There is a plugin that allows you to have "multiple cursors". I haven't tried it myself, but you should check out if it solves your problem:

http://www.vim.org/scripts/script.php?script_id=4523

Idan Arye
  • 12,402
  • 5
  • 49
  • 68