Tuesday, May 12, 2009

Inserting new lines with vim regular expressions

If you want to insert new lines with sed-style regular expressions in vim, the usual '\n' doesn't work. The trick is to produce a '^M' by hitting Ctrl-V,Ctrl-M. For example:


:%s/word /word^M/g


will replace the space after the word "word" with a line break across the whole file.

2 comments:

Lars Tesmer said...

Thank you! Already pulled my hairs out trying to figure out why \n\n wouldn't work! :D

Prashanth said...

Thanks!