热门标签 | HotTags
当前位置:  开发笔记 > 开发工具 > 正文

根据单个字符缩进一段代码

如何解决《根据单个字符缩进一段代码》经验,为你挑选了1个好方法。

该问题的标题听起来可能有点含糊,所以在这里我将更清楚地说明情况。我在文件中有这些代码行,我想相对于字符对齐=

const service = require('./service');
const baseService = require('./baseService');
const cOnfig= require('../config');
const Promise = require('bluebird');
const errors = require('../errors');

我希望以上几行看起来像这样

const service     = require('./service');
const baseService = require('./baseService');
const cOnfig= require('../config');
const Promise     = require('bluebird');
const errors      = require('../errors');

我希望所有=字符都位于同一列中,并相应地更改后续代码。我该怎么做才能完成这项任务?

能够执行此操作的插件会很好,但是如果我无需任何插件的帮助就可以执行此操作。这样我也会学到一些东西。



1> SergioAraujo..:

Using GNU tools

 :%!column -t -s= -o=      

  % ............. current file
  ! ............. use external command
 -t ............. use tabs
 -s ............. input separator
 -o ............. output separator

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/17360801/
" 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 ,call AlignText()
" :Align =
" :8,$ Align =

If you wnat to test this function before put it into your vimrc, copy the code to your clipboard and then try this:

:@+

Now you can use something like this:

:15,22Align /
:Align ,

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/17360801/
" 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 ,call AlignText()


推荐阅读
author-avatar
guoqiuping98_376
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有