VS code中自动对齐特定符号插件“Alignment”介绍【超好用】

概述

介绍一款VS Code超好用的自动对齐插件Alignment,可以自定义快捷键和对齐符号。

安装

扩展商店中安装这个:
在这里插入图片描述

对齐规则

默认对齐规则:可以写在json设置文件中(见文末),一看就懂,参与对齐的符号有(:, ::, =, => 等),你可以自定义新的对齐符号在后面,例如C++注释符号//,MATLAB注释符号%等。

使用

安装完成后,选中要对齐的文本,ctrl + shift + p,输入"align" 可以看到有三个选项:
在这里插入图片描述

  • align to first char (快捷键 shift + alt + =) 只把每一行的第一个特定字符对齐,后面即使还有特定字符也不对齐(对齐程度最低)
  • align all chars (快捷键 alt + =) 把每一行的每一个出现特定字符的地方都对齐
  • align whitespace (快捷键 alt + -) 把每一行的每一个特定字符,连同空格也全部都对齐(对齐程度最高,强迫症狂喜)

例:
原文本:

test = imread(path_to_test);         : this is test image
test002  = imread(path_to_test002);             : this is test002 image
beautiful_map = imread(path_to_beautiful_map);       : this is beautiful_map image
beautiful_map22 = imread(path_to_beautiful_map22);       : this is beautiful_map22 image

test = imread(path_to_test);         % this is test image
test002  = imread(path_to_test002);             % this is test002 image
beautiful_map = imread(path_to_beautiful_map);       % this is beautiful_map image
beautiful_map22 = imread(path_to_beautiful_map22);       % this is beautiful_map22 image

第1 种对齐规则:(shift + alt + =) 虽然=和:都是参与对齐的特定字符,但是只有等号对齐了:

test            = imread(path_to_test);         : this is test image
test002         = imread(path_to_test002);             : this is test002 image
beautiful_map   = imread(path_to_beautiful_map);       : this is beautiful_map image
beautiful_map22 = imread(path_to_beautiful_map22);       : this is beautiful_map22 image

test            = imread(path_to_test);         % this is test image
test002         = imread(path_to_test002);             % this is test002 image
beautiful_map   = imread(path_to_beautiful_map);       % this is beautiful_map image
beautiful_map22 = imread(path_to_beautiful_map22);       % this is beautiful_map22 image

第2种对齐规则:(alt + =) =和:都对齐了:(这种方式应该符合大多数场景)

test            = imread(path_to_test);           : this is test image
test002         = imread(path_to_test002);        : this is test002 image
beautiful_map   = imread(path_to_beautiful_map);  : this is beautiful_map image
beautiful_map22 = imread(path_to_beautiful_map22);: this is beautiful_map22 image

test            = imread(path_to_test);         % this is test image
test002         = imread(path_to_test002);             % this is test002 image
beautiful_map   = imread(path_to_beautiful_map);       % this is beautiful_map image
beautiful_map22 = imread(path_to_beautiful_map22);       % this is beautiful_map22 image

第3种对齐规则:(alt + -) =、:、空格全部对齐:(每一个单词全部严格对齐,十分工整)

test            = imread(path_to_test);                  : this is test            image
test002         = imread(path_to_test002);               : this is test002         image
beautiful_map   = imread(path_to_beautiful_map);         : this is beautiful_map   image
beautiful_map22 = imread(path_to_beautiful_map22);       : this is beautiful_map22 image

test            = imread(path_to_test);                  % this is test            image
test002         = imread(path_to_test002);               % this is test002         image
beautiful_map   = imread(path_to_beautiful_map);         % this is beautiful_map   image
beautiful_map22 = imread(path_to_beautiful_map22);       % this is beautiful_map22 image

附:上述其实是MATLAB代码,%表示注释,但是在Alignment插件的默认规则中%不是特定符号,因此需要我们手动添加%成为对齐符号。方法:ctrl + shift + p,输入setting json,打开open user settings (json)
在这里插入图片描述
在一级花括号下,结尾处添加setting内容(注意,如果原先没有“alignment.chars"这块内容,需要在它上面的那块内容后面加分号; 不然json格式报错):

    "alignment.chars": {
        
        ":": {
            "spaceBefore": 0,
            "spaceAfter": 1
        },
        "::": {
            "spaceBefore": 0,
            "spaceAfter": 0
        },
        "=": {
            "spaceBefore": 1,
            "spaceAfter": 1
        },
        "===": {
            "spaceBefore": 1,
            "spaceAfter": 1
        },
        "==": {
            "spaceBefore": 1,
            "spaceAfter": 1
        },
        "=>": {
            "spaceBefore": 1,
            "spaceAfter": 1
        },
        "+=": {
            "spaceBefore": 1,
            "spaceAfter": 1
        },
        "-=": {
            "spaceBefore": 1,
            "spaceAfter": 1
        },
        "*=": {
            "spaceBefore": 1,
            "spaceAfter": 1
        },
        "/=": {
            "spaceBefore": 1,
            "spaceAfter": 1
        },
        "%": {
            "spaceBefore": 1,
            "spaceAfter": 1
        }

注意,上面的最后一小部分"%"是我添加的,这样%也被当作特定符号参与对齐了。
设置好了以后,保存json文件即可,再试一下第2种对齐规则:(alt + =),现在发现 =、:、%三者全部都对齐了:

test            = imread(path_to_test);           : this is test image
test002         = imread(path_to_test002);        : this is test002 image
beautiful_map   = imread(path_to_beautiful_map);  : this is beautiful_map image
beautiful_map22 = imread(path_to_beautiful_map22);: this is beautiful_map22 image

test            = imread(path_to_test);           % this is test image
test002         = imread(path_to_test002);        % this is test002 image
beautiful_map   = imread(path_to_beautiful_map);  % this is beautiful_map image
beautiful_map22 = imread(path_to_beautiful_map22);% this is beautiful_map22 image

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值