如何插入空格到X列排列列中的东西?

我有我的源代码复制操作符写如下。

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

对于你的情况,你需要select你的expression式,然后input:Align = 。 它将alignment所有内容,使用=作为分隔符和引用。

(有很多选项可以alignment,左alignment,右alignment,循环等)

您也可以检查提供类似function的Tabular.vim 。 看到那里的屏幕截图演示。

这里的其他答案是非常棒的,特别是@nelstrom对Tabular.vim和他的优秀截屏video的评论。

但是如果我懒得安装任何Vim插件,但愿意使用Vimmacros,我会使用macros。

algorithm:

 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; 

将光标定位在第一行的任何位置,并通过在正常模式下键入来logging该行的macros:

 qa0f=100i <Esc>8|dwjq 

其转化为:

  1. qa – 用热键alogging一个macros
  2. 0 – 转到行首
  3. f= – 转到第一个等号
  4. 100i <Esc> – (在i之后有一个单独的空格, <Esc>表示按下escape,不要键入“<Esc>”。)插入100个空格
  5. 8| – 转到第8列(对不起,您必须手动确定要alignment的列)
  6. dw – 删除直到下一个非空格字符
  7. j – 转到下一行
  8. q – 停止录制。

然后运行存储在热键a的macros,3次(对于剩余的3行),将光标放在第二行,然后按:

 3@a 

如果您使用的是类似unix的环境,则可以使用命令行工具column 。 使用可视模式标记您的线条,然后:

 :'<,'>!column -t 

这会将选定的文本粘贴到命令'<,'>! std '<,'>!标签之后'<,'>! 。 请注意'<,'>! 在视觉模式下点击时自动插入。

我们可以使用这两个函数,我在下面的path中描述了相同的情况: https : //stackoverflow.com/a/32478708/3146151

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

我已经发布在这里的function: https : //github.com/imbichie/vim-vimrc-/blob/master/MCCB_MCCE.vim

我们需要在vim编辑器中调用这个函数,并给出你想要移动的字符或空格的出现次数以及“'和列号中的字符。

出现次数可以从每行的开始(MCCBfunction)开始,也可以在每行的末尾(MCCEfunction)。

对于上面提到的问题我们可以使用MCCB函数和我们可以使用'='的字符,所以在vim编辑器中的用法是这样的。

 :1,4call MCCB(1,'=',8) 

所以这会把第一个=号从第一行移到第八行。

这些是function:

 " 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 

快速简单的方法是添加X空格,然后删除回X列。例如,如果X = 40,则键入

 40a<Space><Esc>d40| 

我知道这是旧的,但我认为@talklittle有正确的想法,答案只是得到了详细。 更快的方法是在=之后插入空格,然后在第10列之后删除所有空格,如下所示:

  :1,4 s/^\(.*=\) *\(.*\)$/\1 \2/ :1,4 s/^\(.\{10\}\) *\(.*\)$/\1\2/