1

There comes a time where I'm in a situation of downloading multiple files. And the name of files are in series. Something like.

http://www.example.com/files/images/img01.jpg
http://www.example.com/files/images/img02.jpg
http://www.example.com/files/images/img03.jpg
...

and so on.

Suppose there are files from img01.jpg to img49.jpg. What is the best way to write the full URL of the image on each line (as done above) without repetition? So that I can pass wget the list with -i parameter?

P.S.: Don't tell me I can do curl http://www.example.com/files/images/img{01,49}.jpg in bash to accomplish my work.

  • 7
    You can do curl http://www.example.com/files/images/img{01,49}.jpg in bash to accomplish your work. – romainl Nov 25 '15 at 6:13
  • @romainl You forced me to sign up to reply to your comment. Now let me tell you that I'm using Windows. and { & } are bash expansion. So no luck on Powershell, at least. – Santosh Kumar Nov 25 '15 at 6:33
  • Well, you will need to log in to accept my answer anyway so… – romainl Nov 25 '15 at 10:18
  • I got a problem with my accounts: meta.stackexchange.com/q/269958/181377 – Santosh Kumar Nov 25 '15 at 18:38
12

Here is a different version, that assumes you have Vim 7.4.754 (25 Jun 2015) or newer:

If you have this line:

http://www.example.com/files/images/img01.jpg

Now copy and paste it 48 times yy48p. Then go to the second line, visually block select the 01 down to the bottom and press g<C-A>

You might need to adjust your 'nrformat' settings as romainl said in his answer.

12
  1. Assuming you have this line:

    http://www.example.com/files/images/img01.jpg
    
  2. Start recording in some alphabetic register, say q:

    qq
    
  3. Yank the line:

    yy
    
  4. Put it below the current line:

    p
    
  5. Increment the number:

    <C-a>
    
  6. Stop the recording:

    q
    
  7. Play it back 47 times:

    47@q
    

In short:

qqyyp<C-a>q47@q

Note that you will almost certainly need to tell Vim to stop considering numbers starting with 0 as octal numbers:

:set nrformats-=octal
  • Your answer was cool too, but I chose Christian's because it involves fewer keystrokes. :) – Santosh Kumar Nov 26 '15 at 19:18
2

Another option is to use :substitute command together with sub-replace-\=, and using the line numbers to get the increments.

You can start with the line

http://www.example.com/files/images/img01.jpg

and duplicate it enough times with the yy48p. After that, the substitute command would be

:%s/01/\=printf('%02d', line('.'))

A more general form to perform the same action is

%s/img\zs\d\{2}/\=printf('%02d', line('.'))

Where you use \zsto anchor the number reference to certain location in the pattern (if there were multiple numbers) and don't need to necessarily have the same number on each line when you start the substitution. You can also include an offset if needed:

%s/img\zs\d\{2}/\=printf('%02d', line('.')+4)
  • It's too much for this trivial problem. :P By the way thanks for the answer. – Santosh Kumar Nov 27 '15 at 11:41
2

Like Cajanus, start with one line:

http://www.example.com/files/images/img01.jpg

and duplicate it 48 times:

yy48p

Using visincr.vim, use ctrl-v to visual-block select the 01s. Then

:II 1 0

You can get visincr.vim from

http://www.drchip.org/astronaut/vim/index.html#VISINCR

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.