概述
介绍一款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