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

Related Post

Go to the Previous Directory in Vim I use Vim. Vim is the most productive text editor if you know some vim commands When I open a file and I am done with it. I often want to go...
Export Data From Firebird Database to CSV File Background Some software applications use a database called Firebird. This is an open source database. If the software application is compiled you ca...
Installing Ruby on Ubuntu Installing Ruby on Ubuntu As seen on RVM... Go to the terminal in Ubuntu. Add the public key from RVM $ gpg --keyserver hkp://keys.gnupg.n...
Installing Hipchat on Ubuntu Tested on: Ubuntu 12.04 LTS Thinkpad T400, 2GB RAM Intel Core 2 Duo CPU P8600, 32bit Sources: Official hipchat linux instructions Ask U...

Leave a Reply