问题描述
|
我将复制操作员的源代码编写如下。
foo = rhs.foo;
foobar = rhs.foobar;
bar = rhs.bar;
toto = rhs.toto;
我想按如下顺序排列(更易于阅读,不是吗?)。
foo = rhs.foo;
foobar = rhs.foobar;
bar = rhs.bar;
toto = rhs.toto;
是否有VIM魔术插入列N,或者类似的东西使我可以使用每行几次击键来排列东西?
解决方法
有一个很好的插件可以执行此操作以及更多操作,称为Align.vim。
对于您的情况,您需要选择表达式,然后键入 \”。)插入100个空格
:Align =
。它将使用=
作为分隔符和参考对齐所有内容。
(有很多选项可以对齐,左对齐,右对齐,循环对齐等)
您也可以检查提供类似功能的Tabular.vim。请参阅此处的截屏视频以获取演示。
, 这里的其他答案很好,尤其是@nelstrom对Tabular.vim的评论以及他出色的截屏视频。
但是,如果我懒得安装任何Vim插件,却又不愿意使用Vim宏,我会使用宏。
算法:
For each line,Add tons of spaces before the symbol =
Go to the column you want to align to
Delete all text up to =,thereby shifting the = into the spot you want.
例如,
foo = rhs.foo;
foobar = rhs.foobar;
bar = rhs.bar;
toto = rhs.toto;
将光标放在第一行的任何位置,并在正常模式下通过键入来记录该行的宏:
qa0f=100i <Esc>8|dwjq
转换为:
qa
-在热键a
中记录宏
0
-转到行首
f=
-转到第一个等号
100i <Esc>
-(在i
之后有一个空格,而<Esc>
表示按转义键,请勿键入\“ 8|
-转到第8列(对不起,您必须手动找出要对齐的列)
dw
-删除直到下一个非空格字符
j
-转到下一行
q
-停止录音。
然后,将光标移到第二行并按以下键,将存储在热键a
的宏运行3次(对于剩余的3行)。
3@a
, 如果使用的是类似Unix的环境,则可以使用命令行工具column
。使用可视模式标记线条,然后:
:\'<,\'>!column -t
这会将选定的文本粘贴到\'<,\'>!
之后的命令的标准输入中。请注意,在可视模式下按:
时,会自动插入\'<,\'>!
。
, 快速,简单的方法是添加X空格,然后删除回到X列。例如,如果X = 40,则键入
40a<Space><Esc>d40|
, 对于同一场景,我们可以使用以下路径中描述的这两个功能:https://stackoverflow.com/a/32478708/3146151
只需将这两个函数放在.vimrc或.gvimrc中,并在需要时在编辑器中将这些函数作为普通函数调用即可。
我在这里发布的功能:https://github.com/imbichie/vim-vimrc-/blob/master/MCCB_MCCE.vim
我们需要在vim编辑器中调用此函数,并给出要移动的字符或空格的出现次数,以及\'\'内的字符和列号。
发生次数可以从每行的开头(MCCB功能)开始,也可以在每行的末尾(MCCE功能)开始。
对于问题中提到的上述示例,我们可以使用MCCB函数和字符,我们可以使用\'= \',因此用法在vim编辑器中将是这样。
:1,4call MCCB(1,\'=\',8)
因此,这会将第一个“ 3”符号从行号1移至第8列。
这些是功能:
\" 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,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,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:lastline)
endif
endfunction
, 另一种解决方案是执行两个连续的替换:
%s/=/ =/
%s/\\%>7c *//
诀窍是只有在第7列之后,列模式\\%>7c
才与空白
pattern32ѭ相匹配。这里的foobar
是最长的变量名称,带有6
个字符,因此在正则表达式中需要7
。
, 我知道这很老了,但我认为@talklittle有正确的主意,答案很冗长。一种更快的方法是在=后面插入空格,然后在第十列之后删除所有空格,如下所示:
:1,4 s/^\\(.*=\\) *\\(.*\\)$/\\1 \\2/
:1,4 s/^\\(.\\{10\\}\\) *\\(.*\\)$/\\1\\2/