Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to find and replace caret open_parentheses some_content close_parentheses (i.e.^(.*)) with caret open_curly_bracket the_same_content close_curly_bracket (i.e. ^{.*} but I have issues.

1) I do not understand how to preserve the matched wildcard content .*

2) I do not see how to formulate the escape sequence to specify curly brackets and parentheses.

I have been going through this page for quite a bit and have attempted the following

%s/^(*)/^{*} /gc
%s/^(.*)/^{.*} /gc
%s/^/(.*/)/^{.*} /gc
share|improve this question
What carrots? Did you mean caret? – kinokijuf Jul 22 '12 at 20:24
Yes i did mean caret! – rudolph9 Jul 22 '12 at 20:28
up vote 5 down vote accepted

All of those characters have special functionality depending if an escape character precedes them. This depends on the character whether an escape sequence preceding the character with special functionality utilises it (i.e. a ^ executes special functionality, a \^ does not, where a ( does not execute special functionality, and a \( does.

The expression you are looking for is as follows


The expression literally state, match a caret followed by an open_parenthesis followed by any character except a close_parenthesis followed by a close_parenthesis and replace with a a caret followed by an open_curly_bracket followed by the content found when searching for any_character_except a close_parenthesis (i.e. the content between \( and \)) followed by a close_curly_bracket

Note: other approaches exist as well

share|improve this answer
How do I say the first occurrence of the of the ')' close_parentheses? – rudolph9 Jul 22 '12 at 16:18
The regex works but matched the last close_parentheses before the newline character. – rudolph9 Jul 22 '12 at 16:24
There are two ways to do that. One is to replace * by \{-} to match as few . as possible. See :help /\{-. The other is to replace . by [^)] to match anything not a ). See :help /[]. – garyjohn Jul 22 '12 at 16:54
Yeah, I forgot about the greedy match thing. Thanks @kinokijuf for editing my answer! – fluffy Jul 23 '12 at 17:08
@fluffy but i only corrected several typos – kinokijuf Jul 23 '12 at 18:11

Are you really looking for the caret character?
The caret usually means to start looking for the pattern at the beginning of a line.

Assuming you are...

some text ^( more text ) and more ^( more text ) and more

--AND-- you what it changed to:

some text ^{ more text } and more ^{ more text } and more

Then, use:


:s      the substitute command
-       start pattern, use - instead of / for clarity
\^      look for the ^ char, need to escape
(       followed by (
\(      start capture
[^)]\+  one or more chars except )
\)      end capture
)       followed by )
-       start replace, use - instead of / for clarity
^{      replace ^( with ^{
\1      replace with the captured text
}       replace ) with }
-       start flags, use - instead of / for clarity
gc      confirm replace for each occurance in the line

If you want the search and replace to span the whole file, then use:


The % means the whole file.

share|improve this answer
My assumption in this question is that he's looking to replace regexps with other regexps, using a regexp. Always tricky. :) – fluffy Jul 23 '12 at 17:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .