Vim Regex Cheat Sheet

(Note: This is a work in progress)

Sublime used the PCRE engine (Perl Compatible Regular Expressions)

Vim uses its own Vim regex engine

Python uses Re. Another regex engine.

While the basics of regex are the same. The engines are not the same.

I use Sublime when files are not so large. Up to 1MB. Beyond that I use Vim.

For ref. Vim search and replace: :%s/find/replace/g. Find matches on every line.

For ref. Vim search before replacing: Type /.

This is a regex cheat sheet of things I do often.

Find any character up to the first character match

Aka non-greedy match.

Example: <span style="font-size:13px">

I want to match the opening tag and every character up to the first closing tag.

Sublime: <.*?>

Vim: <.\{-}>

So instead of using .*? in Sublime. I use .\{-} in Vim.

Match n times or n to m times

Sublime: {1,2}

Vim: \{1,2}

Replace something with a new line

Sublime: \n

Vim: \r

Group patterns and reuse them

Sublime: (group in parenthesis). Reuse with: $1

Vim: \(escape open and close parenthesis\). Reuse with: \1

Removing Carriage Return ^M

Vim: :%s/\r//g

Leave a Reply