Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(g)vim replace regex

Tags:

regex

vim

I'm looking for a regex that will change sth. like this:

print "testcode $testvar \n";

in

printnlog("testcode $testvar \n");

I tried %s/print\s*(.\{-});/printnlog(\1);/g but gvim says

print\s*(.\{-});

doesn't match.

Where is my fault? Is it ok to use '*' after '\s' because later '{-};' will stop the greed?

Thanks in advance.

like image 569
Milde Avatar asked Dec 04 '09 14:12

Milde


People also ask

How do I change a pattern in Vim?

This option is useful when you have the '/' character in the search pattern or the replacement string. Press y to replace the match or l to replace the match and quit. Press n to skip the match and q or Esc to quit substitution. The a option substitutes the match and all remaining occurrences of the match.

How do I search and replace in Vim?

The simplest way to perform a search and replace in Vim editor is using the slash and dot method. We can use the slash to search for a word, and then use the dot to replace it. This will highlight the first occurrence of the word “article”, and we can press the Enter key to jump to it.

How do I find and replace one by one in Vim?

Use the slash and dot command to find and replace a single occurrence of a word in Vim.

Does Vim use regex?

Using Regex in VimMany of Vim's search features allow you to use regular expressions. Some of them are: The search commands ( / and ? ) The global and substitute command-line (ex) commands ( :g and :s )


1 Answers

In vim you have to prepend (, ) and | with backslash, so try

:%s/print\s*\(.\{-}\);/printnlog(\1);/g
like image 182
MBO Avatar answered Oct 21 '22 20:10

MBO