10

There are a surprising number of questions on this subject, yet some how none of them answer what I want.

I have a code block as follows:

void foo {

somecodehere
morecode
...

}

As you can see the guts of my function are indents on the same level as the rest of my code block. What I currently do is >% on the end of my block (ie the }) which indents the entire block, including the lines with my curly braces.

What I'd like to do is a similar command which indented every line in between excluding my curly braces.

I can not begin to tell you how much time this would save me.

Community
  • 1
  • 1
tzenes
  • 1,651
  • 2
  • 15
  • 30
  • 1
    Use `=` for formatting (not indenting, but for me gives same or better results). Unless you don't like the way vim formats your code. `gg=G` will format the entire file. – FrustratedWithFormsDesigner Sep 29 '10 at 15:03
  • @Frustrated not exactly what I'm looking for (as sometimes I don't want to mess with the inner formatting), but also helpful to know. So +1 to you sir – tzenes Sep 29 '10 at 15:36

4 Answers4

15

Rather than mapping something new, >i{ seems to do what you want here, but it moves the cursor.

Randy Morris
  • 36,853
  • 6
  • 63
  • 73
3

Put the cursor inside the block you want to indent, and type vi{>

v enters visual mode.

i{ selects everything inside the innermost {} block

> indents the visual block and goes back into normal mode

You can then use '' to move the cursor back to its original line.

N.B. if you want to indent the braces as well then replace i{ with a{.

Dave Kirby
  • 23,068
  • 5
  • 60
  • 79
1

insert

map sb v%><<%<<

into your .vimrc (replace 'sb' with any key combination you want). it shifts the whole function to the right and then the first and the last line back to the left. It works in the first or on the last line of a block

Nikolaus Gradwohl
  • 17,696
  • 2
  • 43
  • 60
1

I use marks which can be a pain sometimes but it works.
Go to the last line of your function (the line above the })
ma Create mark 'a'
Go back to the top of the function and do this.

>`a

That just says indent by one level until it reaches mark a.

jonescb
  • 19,527
  • 6
  • 44
  • 41