Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to indent lines automatically?

Tags:

vim

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?

like image 249
Mosh Avatar asked Dec 20 '25 00:12

Mosh


2 Answers

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;

like image 176
Mark Rushakoff Avatar answered Dec 21 '25 21:12

Mark Rushakoff


:set cindent
like image 30
chaos Avatar answered Dec 21 '25 20:12

chaos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!