如何在gVim中以我想要的方式缩进C代码?

时间:2015-09-22 04:29:18

标签: vim indentation

我在 *。c 文件中

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

我想缩进如下:

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

所有等号在一列中对齐。我正在寻找一个通用命令来实现这一目标。

3 个答案:

答案 0 :(得分:2)

如果您经常这样做,可以使用vim插件Tabular来执行此操作。

在可视模式下,选择要缩进的内容并键入:Tab /=

答案 1 :(得分:1)

我认为这就是你想要的,我们可以使用我在下面的路径中为同一场景描述的两个函数:https://stackoverflow.com/a/32478708/3146151

只需将这两个函数放在.vimrc或.gvimrc中,并在编辑器中随时调用函数作为普通函数调用。

我在此处发布的功能:https://github.com/imbichie/vim-vimrc-/blob/master/MCCB_MCCE.vim

我们需要在vim编辑器中调用此函数,并给出你要移动的角色或空间的出现次数以及'中的角色。 '和列号。

出现的次数可以从每一行的起点(MCCB功能)开始,也可以在每一行的末尾(MCCE功能)。

对于问题中提到的上述示例,我们可以使用MCCB函数和我们可以使用的字符&#39; =&#39;,因此在vim编辑器中的用法将如此。< / p>

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

因此,这会将第一个=符号从第1行移动到第13列。

这些是功能:

" 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

答案 2 :(得分:1)