5

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?

2 Answers 2

2

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]
2
  • 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, 2012 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, 2012 at 9:55
3

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

You must log in to answer this question.

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