VSCode Linux的C++代碼格式化配置的實現(xiàn)
1、安裝clang-format工具
命令
~$ sudo apt-get install clang-format
安裝后,查找安裝的地址:
命令
~$ which clang-format
得到安裝地址:/usr/bin/clang-format
2、配置代碼格式化
2.1 全局用戶配置格式化代碼
(1)進入目錄全局vscode的setting.json目錄,命令:
cd ~/.config/Code/User/settings.json
(2)編譯文件,命令:
vi settings.json
在原文檔最后內(nèi)容后面添加以下選項:
"editor.formatOnSave": true, "clang-format.executable": "/usr/bin/clang-format",
然后,重新打開VSCode,按下Ctrl+S時會自動格式化代碼。
2.2 工作區(qū)配置格式化代碼
(1)進入當前代碼工作區(qū),按下快捷鍵 Ctrl+Shift+P或者點擊右下角的設置按鍵,選擇Command Palete,輸入settings.json如下:
(2)在settings.json輸入配置命令:
{ "editor.formatOnSave": true, "clang-format.executable": "/usr/bin/clang-format", }
保存后,在當前工程下面,按下Ctrl+S保存時會自動格式化代碼。
代碼測試
a>格式化前:
#include <iostream> int main() { int a = 1; int b = 2; int c = 3; return 0; }
b>格式化后:
#include <iostream> int main() { int a = 1; int b = 2; int c = 3; return 0; }
(3)不用保存時格式化代碼,使用快捷鍵格式化代碼
如果不想在保存時格式化代碼,可以去掉上面的配置,如下:
{ //"editor.formatOnSave": true, "clang-format.executable": "/usr/bin/clang-format", }
默認的格式化代碼快捷鍵Ctrl+Shift+I
如下:
附加:
1、格式化代碼配置方法也可參考:
2、VSCode的插件下載官網(wǎng)地址:Extensions for Visual Studio family of products | Visual Studio Marketplace
到此這篇關于VSCode Linux的C++代碼格式化配置的實現(xiàn)的文章就介紹到這了,更多相關VSCode C++代碼格式化配置內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C++線程優(yōu)先級SetThreadPriority的使用實例
這篇文章主要介紹了C++線程優(yōu)先級SetThreadPriority的使用實例,較為詳細的講述了C++線程及其優(yōu)先級的用法,需要的朋友可以參考下2014-10-10