cloudrain21
cloudrain21

Reputation: 659

How can I adjust indentation of my c source code in the entire selected block all at once?

I'd like to adjust indentation of my source code correctly at a time after I select some block of it. Is there any function or key with which I can do it including parenthesis?

Here is original selected block of sample code I'd like to adjust indentation.

while(1)
{
    func1();
    if( )
    {
        func2();
       } 
            }

if( x == 0 )
  {
      aa = 1;
  }

This would be the correctly indented code how I just want to adjust.

while(1)
{
    func1();
    if( )
    {
        func2();
    } 
}

if( x == 0 )
{
    aa = 1;
}

Upvotes: 0

Views: 100

Answers (2)

cloudrain21
cloudrain21

Reputation: 659

I'm using evil mode because I like vim editing keymap. In my case, block auto indentation can be done by equal(=) key after selecting a code block. It's very convenient to rearrange block of code in a c-default-style.

(1) install evil package
(2) Insert this code into you emacs init file.
; indentation style for c, c++, java
(setq c-default-style "linux"
      c-basic-offset 4)
(3) select block using v and direction key
(4) press '='

Upvotes: 0

Chris
Chris

Reputation: 136918

Select your code and press C-M-\, which should be bound to indent-region:

C-M-\

Indent all the lines in the region, as though you had typed TAB at the beginning of each line (indent-region).

If a numeric argument is supplied, indent every line in the region to that column number.

Upvotes: 3

Related Questions