Sagar Jain
Sagar Jain

Reputation: 7941

How to indent C code the way I want in gVim?

I've this in a *.c file:

Temperature = MAX_TEMP;
Pressure = 90;
FuelLevel = LOW;

I want to indent it as below:

Temperature = MAX_TEMP;
Pressure    = 90;
FuelLevel   = LOW;

All equal signs aligned in a single column. I'm looking for a generalized command to achieve this.

Upvotes: 0

Views: 143

Answers (3)

Lirian Su
Lirian Su

Reputation: 346

If you do this very often, you can use vim plugin Tabular to do this.

In Visual mode, select what you want to indent and type:Tab /=

Upvotes: 2

imbichie
imbichie

Reputation: 1606

I think this is what you want, We can use two functions that I described in the below path for the same scenario : https://stackoverflow.com/a/32478708/3146151

simply put those two functions in your .vimrc or .gvimrc and call the functions as normal function call in your editor whenever you want.

The functions I have posted it here : https://github.com/imbichie/vim-vimrc-/blob/master/MCCB_MCCE.vim

We need to call this function in vim editor and give the Number of Occurrence of the Character or Space that you wants to move and the character inside the ' ' and the column number.

The number of occurrence can be from the starting of each line (MCCB function) or can be at the end of each line (MCCE function).

for the above example mentioned in the question we can use the MCCB function and the character we can use '=', so the usage will be like this in the vim editor.

:1,3call MCCB(1,'=',13)

So this will move the first = sign to the 13th column from line number 1 to 3.

These are the functions :

" MCCB - Move the Character to the Column from the Begin of line
" This is a function for Moving the specified Character 
" in a given range of lines to a the specified Column from the Begin of the line
" NOTE 1 :- If the specified character and the first character of the line are same
"           then the number of Occurance (num_occr) will be one less than the actual
" NOTE 2 :- Maximum space between the specified character with in the range 
"           of lines should be less than or equal to 80, if we need more than 80
"           then we need to insert more spaces by increasing the value 80 in the 
"           "nmap s 80i <ESC>" line inside the function
" Usage :-  in command mode do it like below
" Eg 1:-    :5,11call MCCB(1, '=', 8)
"           The above command will move the 1st Occurance from the begin of Character =
"           to the 8th Column of the lines from 5 to 11
" Eg 2 :-   :7,10call MCCB(2, '+', 12)
"           The above command will move the 2nd Occurance of Character = to the 12th
"           Column of the lines from 7 to 10
    function! MCCB (num_occr, mv_char, col_num) range
        if (a:firstline <= a:lastline)
            nmap s 80i <ESC>
            let line_num = a:firstline
            while line_num <= a:lastline
                execute "normal " . line_num . "G0" . a:num_occr . "f" . a:mv_char . "s" . a:col_num . "|dw"
                let line_num = line_num + 1
            endwhile
            nunmap s
        else
            execute printf('ERROR : Start line %d is higher thatn End line %d, a:firstline, a:lastline)
        endif
    endfunction

" MCCE - Move the Character to the Column from the End of line
" This is a function for Moving the specified Character 
" in a given range of lines to a the specified Column from the End of the line
" NOTE 1 :- If the specified character and the last character of the line are same
"           then the number of Occurance (num_occr) will be one less than the actual
" NOTE 2 :- Maximum space between the specified character with in the range 
"           of lines should be less than or equal to 80, if we need more than 80
"           then we need to insert more spaces by increasing the value 80 in the 
"           "nmap s 80i <ESC>" line inside the function
" Usage :-  in command mode do it like below
" Eg 1:-    :5,11call MCCE(1, ';', 20)
"           The above command will move the 1st Occurance from the End of Character ;
"           to the 20th Column of the lines from 5 to 11
" Eg 2 :-   :7,10call MCCE(5, 'i', 26)
"           The above command will move the 5th Occurance from the End of Character i
"           to the 26th Column of the lines from 7 to 10
    function! MCCE (num_occr, mv_char, col_num) range
        if (a:firstline <= a:lastline)
            nmap s 80i <ESC>
            let line_num = a:firstline
            while line_num <= a:lastline
                execute "normal " . line_num . "G$" . a:num_occr . "F" . a:mv_char . "s" . a:col_num . "|dw"
                let line_num = line_num + 1
            endwhile
            nunmap s
        else
            execute printf('ERROR : Start line %d is higher thatn End line %d, a:firstline, a:lastline)
        endif
    endfunction

Upvotes: 1

Related Questions