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 code some Objective-C in Vim and one thing I'm missing from Xcode is the ability to auto insert the left matching bracket.

Say if I have the following text:

NSString *string = [NSString alloc] init

and I insert ']' at the end of the row, then I want it to complete to:

NSString *string = [[NSString alloc] init]

Any ideas?

share|improve this question
up vote 2 down vote accepted

That's a nice feature.

Apart from using Surround, I don't know of an exact solution.

With surround, starting with the cursor on the t of init in NSString *string = [NSString alloc] init:

  1. v to enter visual mode
  2. F[ to select everything up to the first [
  3. s] to surround it with []

vF[s] can be tedious in the long run, in absence of a better/smarter solution you could make a dumb mapping like:

inoremap ]] vF[s]
share|improve this answer
That's a pretty neat solution. I'll accept that answer. Please note that it's capital S to surround it seems. – Ciryon Feb 2 '12 at 9:35
It depends on the version: in mine s and S are interchangeable for character-wise selection but different for line-wise selection. – romainl Feb 2 '12 at 9:55

This plugin might be what you're looking for. Haven't tried it out properly myself yet, but it seems to do the job.

For instance, where | is the cursor:

"foo|" becomes "[foo |]" after ] is pressed.
"foo bar|" becomes "[foo bar]|"
"foo: bar|" becomes "foo: [bar |]"
"foo bar: baz|" becomes "[foo bar: baz]|"

Certain useful keywords are also wrapped intelligently

share|improve this answer

You must log in to answer this question.

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