Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Question

I'm going to be sorting through a large mass of html files and replacing links to an ftp site with local links. I am using vim and I am trying to figure out a pattern to use with %s/find/replace/g that will match a string such as

href="ftp://some/directory/to/a/random.pdf"

and replace it with

href="local/directory/random.pdf"

essentially replacing everything except the file name itself. There will be multiple ftp directories in a single html file so the pattern should be able to match any type of ftp link. All of these ftp files will be going into the same local directory. Although most of the files are pdfs some are ptts and other random files.

Are Vim's patterns able to match something like this, and if so what would that pattern look like?

Answer

@akira did a great job with the regex supplied in his answer. I went out on my own to find a way to put this into a reusable function and this is what I came up with:

(inside your vimrc) define a function that allows you to pass a string to replace the matched string

function SwitchFtp(local)
    execute ':%s,href="ftp://.*/\(.\+\)",href="'.a:local.'\1",g'
endfunction

(optional) assign this function to a command so that you don't have to use call

command -nargs=1 SwitchFtp call SwitchFtp(<f-args>)

This would then be called with something like

:SwitchFtp local/directory/
share|improve this question
    
so, "replace everything from 'ftp://' up to the last '/'" ? –  akira Mar 14 '12 at 19:28
    
yes, without matching the filename itself if that's possible. –  mcmullins Mar 14 '12 at 19:30
add comment

2 Answers

up vote 1 down vote accepted
:%s,href="ftp://.*/\(.\+\)",href="local/directory/\1",g
share|improve this answer
    
I updated the question with some clarification. There will not be a common directory on the ftp site, these files will be linked from multiple directories, hence the need for a pattern. –  mcmullins Mar 14 '12 at 19:25
    
haha, that's amazing. Now to try to turn this thing into a function (help's appreciated, but the pattern is a good start). Thanks for the help! I'll have to look into how you did this later. Lots of stuff I haven't seen yet in it. –  mcmullins Mar 14 '12 at 19:52
add comment

To take

href="ftp://some/directory/to/a/random.pdf"

and replace it with

href="local/directory/to/a/random.pdf"

you want

:s#ftp://some/#local/#
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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