如何在 gVim 中按照我想要的方式缩进 C 代码?
How to indent C code the way I want in gVim?
我在 *.c 文件中:
Temperature = MAX_TEMP;
Pressure = 90;
FuelLevel = LOW;
我想缩进如下:
Temperature = MAX_TEMP;
Pressure = 90;
FuelLevel = LOW;
所有等号在一列中对齐。我正在寻找一个通用命令来实现这一点。
我想这就是你想要的,我们可以使用我在以下路径中描述的两个函数来处理相同的场景:
只需将这两个函数放在您的 .vimrc 或 .gvimrc 中,并在您需要时随时在您的编辑器中像普通函数调用一样调用这些函数。
我在这里发布的功能:https://github.com/imbichie/vim-vimrc-/blob/master/MCCB_MCCE.vim
我们需要在vim编辑器中调用此函数,并给出要移动的字符或Space的出现次数以及' '内的字符和列号。
出现的次数可以从每行的开头开始(MCCB函数),也可以从每行的结尾开始(MCCE函数)。
上面问题中提到的例子我们可以使用MCCB功能和字符我们可以使用'=
',所以在vim编辑器中的用法是这样的。
:1,3call MCCB(1,'=',13)
所以这会将第一个 = 符号移动到第 13 列,从第 1 行到第 3 行。
这些是功能:
" 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
如果你经常这样做,你可以使用 vim 插件 Tabular 来做到这一点。
在可视模式下,select 您要缩进和键入的内容:Tab /=
您也可以尝试vim-easy-align or Align : Help folks to align text, eqns, declarations, tables, etc。
我在 *.c 文件中:
Temperature = MAX_TEMP;
Pressure = 90;
FuelLevel = LOW;
我想缩进如下:
Temperature = MAX_TEMP;
Pressure = 90;
FuelLevel = LOW;
所有等号在一列中对齐。我正在寻找一个通用命令来实现这一点。
我想这就是你想要的,我们可以使用我在以下路径中描述的两个函数来处理相同的场景:
只需将这两个函数放在您的 .vimrc 或 .gvimrc 中,并在您需要时随时在您的编辑器中像普通函数调用一样调用这些函数。
我在这里发布的功能:https://github.com/imbichie/vim-vimrc-/blob/master/MCCB_MCCE.vim
我们需要在vim编辑器中调用此函数,并给出要移动的字符或Space的出现次数以及' '内的字符和列号。
出现的次数可以从每行的开头开始(MCCB函数),也可以从每行的结尾开始(MCCE函数)。
上面问题中提到的例子我们可以使用MCCB功能和字符我们可以使用'=
',所以在vim编辑器中的用法是这样的。
:1,3call MCCB(1,'=',13)
所以这会将第一个 = 符号移动到第 13 列,从第 1 行到第 3 行。
这些是功能:
" 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
如果你经常这样做,你可以使用 vim 插件 Tabular 来做到这一点。
在可视模式下,select 您要缩进和键入的内容:Tab /=
您也可以尝试vim-easy-align or Align : Help folks to align text, eqns, declarations, tables, etc。