War2d2
War2d2

Reputation: 247

VIM Convert Text to URL with Search/Replace

I have a document that contains long filenames, followed by a hyphen, followed by a description of the contents of the file. The files are all PDFs. I am converting this document into a page on our website, so that it has the filename, which should be a link to the file, followed by the description of the file contents.

I'm fairly versed in the basics of VIM, but advanced search/replace is something I'm lacking in. What I'd like to do is convert each filename into a link to that filename. For example:

WebAdapt_Prod_Int_10.1_Install.IIS7.2008R2.pdf - Step by step instructions for installing ArcSDE 10.1 for Oracle on the ‘Test’ environment, including configuration notes.

Should convert to:

<a href="documents/WebAdapt_Prod_Int_10.1_Install.IIS7.2008R2.pdf">WebAdapt_Prod_Int_10.1_Install.IIS7.2008R2.pdf</a> - Step by step instructions for installing ArcSDE 10.1 for Oracle on the ‘Test’ environment, including configuration notes.

There are roughly 30 of these documents, so going line-by-line would be time consuming (though by the time I get a response I'll probably already have done it). I'd just like to know how to do this for the next time I'm given a big text file that needs formatting.

Thanks in advance!

Upvotes: 0

Views: 276

Answers (1)

lcd047
lcd047

Reputation: 5861

Try this:

:%s!\v^\S+\.pdf!<a href="&">&</a>!

Please note that the above doesn't try to do anything with HTML entities though. See the Vim Tips wiki for a possible solution if that's a concern.

Edit: The way this works:

  • :% - filter the entire file
  • s!...!...! - substitute
  • \v - set "very magic" syntax for regexps
  • ^\S+\.pdf - match one or more non-spaces at the begging of line, followed by .pdf
  • <a href="&">&</a> - replace with the link: & is the matched string (that is, the filename).

Upvotes: 1

Related Questions