1.字体的修改
Linux 上的 VSCode 写起代码来总让人觉得莫名烦躁,而 Windows 上面的 VSCode 就赏心悦目很多,想了很久之后终于发现是默认字体的问题。Windows 上面的 VSCode 默认是 Consolas 字体,在 Ubuntu 上面大概是 Droid Sans 家族的某种版本,实在是不好看,所以换掉。
首先要在 Ubuntu 上安装 Consolas:
wget https://down.gloriousdays.pw/Fonts/Consolas.zip
unzip Consolas.zip
sudo mkdir -p /usr/share/fonts/consolas
sudo cp consola*.ttf /usr/share/fonts/consolas/
sudo chmod 644 /usr/share/fonts/consolas/consola*.ttf
cd /usr/share/fonts/consolas
sudo mkfontscale && sudo mkfontdir && sudo fc-cache -fv
这样就装好了。用 fc-list 可以看所有安装的字体。
之后在 VSCode的 首选项->设置->字体 中修改对应的项:
'Consolas', 'Droid Sans Mono', 'monospace', monospace, 'Droid Sans Fallback'
这样就可以了
2、Ubuntu环境中,使用VSCode编译器C++代码格式化
2.1在VSCode中下载内置的Clang-Format插件
2.2安装clang-format
在命令行输入sudo apt-get install clang-format
2.3设置自定义的.clang-format文件,并设置自动保存
创建.clang-format文件到工作目录下,如图
在里面自己写好希望保存的代码格式,google的格式,稍微加了一点改动,仅供参考
#基础样式
BasedOnStyle: Google
#指针的*的挨着哪边
PointerAlignment: Right
#访问修饰符前的空格
AccessModifierOffset: -4
# 缩进宽度
IndentWidth: 4
# 连续的空行保留几行
MaxEmptyLinesToKeep: 1
# 圆括号的换行方式
BreakBeforeBraces: Attach
# 是否允许短方法单行
AllowShortFunctionsOnASingleLine: true
# 支持一行的if
AllowShortIfStatementsOnASingleLine: true
# 在未封闭(括号的开始和结束不在同一行)的括号中的代码是否对齐
AlignAfterOpenBracket: true
# switch的case缩进
IndentCaseLabels: true
# 针对OC的block的缩进宽度
ObjCBlockIndentWidth: 4
# 针对OC,属性名后加空格
ObjCSpaceAfterProperty: true
# 每行字符的长度
ColumnLimit: 120
# 注释对齐
AlignTrailingComments: true
# 括号后加空格
SpaceAfterCStyleCast: false
# 换行的时候对齐操作符
AlignOperands: true
# 中括号两边空格 []
SpacesInSquareBrackets: false
# 多行声明语句按照=对齐
AlignConsecutiveDeclarations: false
# 容器类的空格 例如 OC的字典
SpacesInContainerLiterals: false
# 在构造函数初始化时按逗号断行,并以冒号对齐
BreakConstructorInitializersBeforeComma: true
# 函数参数换行
AllowAllParametersOfDeclarationOnNextLine: true
#在续行(# 下一行)时的缩进长度
ContinuationIndentWidth: 4
# tab键盘的宽度
TabWidth: 4
# 赋值运算符前加空格
SpaceBeforeAssignmentOperators: true
# 行尾的注释前加1个空格
SpacesBeforeTrailingComments: 1
2.在settings.json中,加入
"editor.formatOnSave": true
这样即可在保存时自动格式化代码啦~