Andriy Drozdyuk
Andriy Drozdyuk

Reputation: 61091

Perl regex in vim?

How can I write this perl regex replace command in Vim? (Taken from this pandoc epub tutorial):

perl -i -0pe \
's/^Insert\s*(.*)\.png\s*\n([^\n]*)$/!\[\2](..\/figures\/\1-tn.png)/mg' \
*/*.markdown

Upvotes: 2

Views: 1752

Answers (3)

Sam Brinck
Sam Brinck

Reputation: 901

A lot of what you are looking for is dependent on what you "magic" setting is set to. see :help magic for more info on what charctors vim takes literally.

Upvotes: 3

zgpmax
zgpmax

Reputation: 2847

Make your existing one-liner into a pipeline filter

perl -pe 's/^Insert\s*(.*)\.png\s*\n([^\n]*)$/!\[\2](..\/figures\/\1-tn.png)/mg'

Then use 1G!G or :%! in Vim to pipe the current file through that filter, e.g.

:%!perl -pe 's/^Insert\s*(.*)\.png\s*\n([^\n]*)$/!\[\2](..\/figures\/\1-tn.png)/mg'

Upvotes: 0

beerbajay
beerbajay

Reputation: 20270

I can't speak for the -i -0pe flags, but the regex:

s/^Insert\s*(.*)\.png\s*\n([^\n]*)$/!\[\2](..\/figures\/\1-tn.png)/mg

Would be:

s/^Insert\s*\(.*\).png\s*\n\(.*\)$/!\[\2](..\/figures\/\1-tn.png)/g

Note that you have to escape the capturing groups and that I used .* instead of [^\n]* in the second capturing group. You don't need a multi-line flag.

Upvotes: 4

Related Questions