vim regex : how to replace enclosing backticks with tags

Viewed 215

I have this document in Markdown in which i constantly reference keys to be pressed(at the time i wasn't aware of the <kbd> tags!) and have enclosed them in backticks instead. So for example I have a lot of text like `Shift` and `Shift + A`,etc... and I would like to convert them to <kbd>Shift<\kbd> or <kbd>Shift + A<\kbd> instead, using the Vim search and replace options.

Right now, the only way that I think of doing this is by replacing the opening backtick with a <kbd> and then separately replacing the closing backtick with a <\kbd>. But I was wondering if this could be done in a single search and replace command using capture groups or something?

1 Answers

You can use the substitution command:

%s/`\([^`]\+\)`/<kbd>\1<\/kbd>/g

This captures runs between backticks and replaces the inner text with wrapped <kbd> tags.

By the way, </kbd> is the proper HTML closing tag, so I assume you want that instead of <\kbd>.

Related