使用 GNU 工具
:%!column -t -s= -o=
% ............. current file
! ............. use external command
-t ............. use tabs
-s ............. input separator
-o ............. output separator
它可以是一系列行,而不是整个文件1,5
或者您可以选择一个段落vip
我最终创建了一个名为 AlignText 的函数,它使用列命令来解决这个问题:
" Align text by a chosen char
" https://stackoverflow.com/questions/57093175/
" https://vi.stackexchange.com/a/2412/7339
if !exists('*AlignText')
function! AlignText(param) range
execute a:firstline . ',' . a:lastline . '!column -t -s' . a:param . ' -o' . a:param
endfunction
endif
command! -range=% -nargs=1 Align <line1>,<line2>call AlignText(<q-args>)
" :Align =
" :8,$ Align =
如果您想在将其放入 vimrc 之前测试此功能,请将代码复制到剪贴板,然后尝试以下操作:
:@+
现在你可以使用这样的东西:
:15,22Align /
:Align ,
如果您想使用函数调用而不是命令调用,请不要忘记传递范围并将参数放在引号之间。
略有不同的版本,不接受任何参数并仅使用“column -t”
" https://stackoverflow.com/questions/57093175/
" https://vi.stackexchange.com/a/2412/7339
function! AlignText(...) range
if a:0 < 1
execute a:firstline . ',' . a:lastline . '!column -t'
else
execute a:firstline . ',' . a:lastline . '!column -t -s' . a:1 . ' -o' . a:1
endif
endfunction
command! -range=% -nargs=? Align <line1>,<line2>call AlignText(<f-args>)