tags:

views:

135

answers:

4

Quick newbie question. Let's say I have the following code in Vim:

void main()

{

    int i = i + 1;

    return i;
}

I have the cursor on the empty line between the two lines of code. When I press i (or a) to enter text I want to cursor to indent to the right position (i.e. below the i in "int i..."). Any ideas how it can be done?

+2  A: 
:set cindent
chaos
It's not working. I ":set cindent" and as I press a (or i) I begin to enter text on the first column and not on the 4th (or any other column).BTW, I have these in my .Vimrcset autoindentset smartindentset tabstop=4set shiftwidth=4Any ideas?
Mosh
Ah, sorry, missed part of the question. `i` and `a` will never autoindent immediately; their defined behavior is inserting at and after, respectively, the cursor. If you hit enter it will indent, though what I'd recommend you do is use `o` and `O` for starting new lines; that's so ingrained to me that I didn't notice you were doing something else.
chaos
So what is the solution? I have an empty line and I want to start typing on the right (indented) position. Any ideas?
Mosh
Can we forget about the empty line? Just hit `o` on the line above where you want to start typing, or `O` on the line after.
chaos
Well, I guess that some questions have no answer.
Mosh
Yeah, I guess if you absolutely refuse to use `vim` the way it's designed and insist on it behave in a contrary fashion because you're determined to hit one key and not another, then you're out of luck, yeah. Best of luck, don't let the door hit you.
chaos
I don't understand why I deserve you last comment. I asked a simple question, which I don't think is contrary to vim's way of thinking. The situation I describe is simple and happens once in a while. Empty lines (God forbid) do happen in source code once in a while. I perfectly accept that vim can't do everything and what I ask for is not possible. Thank you again.
Mosh
Okay, then let me rephrase. Positioned on your empty line, type `ddO`.
chaos
A: 

You can try typing 3>> while on the int i... line. Not exactly automatic, but it saves time.

Allain Lalonde
+5  A: 

Like @chaos mentioned, cindent is probably what you're looking for.

There's also autoindent, smartindent, and indentexpr, which are all quite configurable and documented at the Vim documentation on indent.

Here's a snippet of how configurable they can be:

{N    Place opening braces N characters from the prevailing indent.
              This applies only for opening braces that are inside other
              braces.  (default 0).

                cino=               cino={.5s           cino={1s
                  if (cond)           if (cond)           if (cond)
                  {                     {                     {
                      foo;                foo;                foo;

Mark Rushakoff
+1  A: 

just use cc on blank lines and o for new lines

JG
I checked this answer. cc works with cindent turned on, ddO works even without. Interesting.
Mosh