Auto-insert left bracket in Vim

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?

Ciryon

Posted 2012-02-01T14:57:57.123

Reputation: 153

Answers

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]

romainl

Posted 2012-02-01T14:57:57.123

Reputation: 19 227

That's a pretty neat solution. I'll accept that answer. Please note that it's capital S to surround it seems. – Ciryon – 2012-02-02T09:35:57.783

It depends on the version: in mine s and S are interchangeable for character-wise selection but different for line-wise selection. – romainl – 2012-02-02T09:55:02.043

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

Lawrence

Posted 2012-02-01T14:57:57.123

Reputation: 31