该问题的标题听起来可能有点含糊,所以在这里我将更清楚地说明情况。我在文件中有这些代码行,我想相对于字符对齐=。
const service = require('./service');
const baseService = require('./baseService');
const config = require('../config');
const Promise = require('bluebird');
const errors = require('../errors');
Run Code Online (Sandbox Code Playgroud)
我希望以上几行看起来像这样
const service = require('./service');
const baseService = require('./baseService');
const config = require('../config');
const Promise = require('bluebird');
const errors = require('../errors');
Run Code Online (Sandbox Code Playgroud)
我希望所有=字符都位于同一列中,并相应地更改后续代码。我该怎么做才能完成这项任务?
能够执行此操作的插件会很好,但是如果我无需任何插件的帮助就可以执行此操作。这样我也会学到一些东西。
Using GNU tools
:%!column -t -s= -o=
% ............. current file
! ............. use external command
-t ............. use tabs
-s ............. input separator
-o ............. output separator
Run Code Online (Sandbox Code Playgroud)
Instead of whole file it can be a range of lines 1,5 or you can select a paragraph with vip
I ended up creating a function called AlignText that uses column command to solve this issue:
" Align text by a chosen char
" /sf/ask/3996522281/
" 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 =
Run Code Online (Sandbox Code Playgroud)
If you wnat to test this function before put it into your vimrc, copy the code to your clipboard and then try this:
:@+
Run Code Online (Sandbox Code Playgroud)
Now you can use something like this:
:15,22Align /
:Align ,
Run Code Online (Sandbox Code Playgroud)
I by any change you want to use the function call instead of the command one, don't forget to pass the range and put the argument between quotes.
A slitly different version that accepts nor arguments and uses just "column -t"
" /sf/ask/3996522281/
" 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>)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
91 次 |
| 最近记录: |