微软的Visual Studio Code(VSCode)编辑器是因为其简洁高效,轻量级,是很多程序员的选择,如何快速上手并使用,本文将对vscode在C/C++下的应用进行简单介绍。
软件下载
建议大家去官方网站按需进行下载:https://code.visualstudio.com/
我们可能会选择下载免安装版本,这样右键菜单会没有使用VScode打开的选项,我们可以使用下面的reg脚本进行注册表添加,新建txt文件,负责下面的脚本,修改其中的路径,然后修改文件后缀为.reg,然后双击执行。
Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\*\shell\VSCode]
@="Open with Code"
"Icon"="D:\\Program Files\\VSCode-Win32-x64-1.70.2\\Code.exe"[HKEY_CLASSES_ROOT\*\shell\VSCode\command]
@="\"D:\\Program Files\\VSCode-Win32-x64-1.70.2\\Code.exe\" \"%1\""Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\Directory\shell\VSCode]
@="Open with Code"
"Icon"="D:\\Program Files\\VSCode-Win32-x64-1.70.2\\Code.exe"[HKEY_CLASSES_ROOT\Directory\shell\VSCode\command]
@="\"D:\\Program Files\\VSCode-Win32-x64-1.70.2\\Code.exe\" \"%V\""Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\Directory\Background\shell\VSCode]
@="Open with Code"
"Icon"="D:\\Program Files\\VSCode-Win32-x64-1.70.2\\Code.exe"[HKEY_CLASSES_ROOT\Directory\Background\shell\VSCode\command]
@="\"D:\\Program Files\\VSCode-Win32-x64-1.70.2\\Code.exe\" \"%V\""
插件安装
安装完VScode后一般需要下载一些插件,这样用着才会顺手,一般你选择编程语言后,VScode会进行推荐,下面试C/C++推荐的一些常用的。
如果我们换电脑了,积累的常用插件可以去对应目录去找,然后复制到新电脑里,实现插件的同步。
vscode插件默认的安装位置是:C:\Users\用户名.vscode\extensions
如果不想把插件安装在C盘的话,可以自己新建一个文件来存储插件,然后在快捷方式的目标中修改路径,在原本的目标后加入–extensions-dir “新的插件存储位置”,例如:“D:\Program Files\Microsoft VS Code\Code.exe” --extensions-dir “D:\Program Files\Microsoft VS Code\extensions”
当然你如果能够联网,那可以使用账户的同步配置功能。
格式化代码
我们可以设置编码风格,然后通过右键的格式化进行编码风格的统一,编码风格可以设置.clang-format文件,Clang_format_style设置为file,将.clang-format文件放到需要设置的文件夹下,VScode会从当前目录网上逐级目录寻找,如果找不到.clang-format文件,则会按照Clang_format_fallback_style设置的进行默认配置。
Clang_format_fallback_style可以设置成VScode模式或者google模式,或者是基于这两个进行简单的修改。
myself { BasedOnStyle: LLVM, AllowShortFunctionsOnASingleLine: Inline, AllowShortIfStatementsOnASingleLine: false, AlwaysBreakTemplateDeclarations: true, SpaceAfterTemplateKeyword: false, ColumnLimit: 120, Standard: Cpp11, IndentWidth: 4, TabWidth: 4, AccessModifierOffset: -4, UseTab: Never, BreakBeforeBraces: Allman }vscode { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Linux, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false }
google { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4, NamespaceIndentation: All, FixNamespaceComments: false }
.clang_format文件
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignConsecutiveMacros: true
# AlignConsecutiveStyle: AcrossEmptyLinesAndComments
AlignEscapedNewlines: Left
AlignOperands: true
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
# AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:AfterCaseLabel: falseAfterClass: falseAfterControlStatement: falseAfterEnum: falseAfterFunction: falseAfterNamespace: falseAfterObjCDeclaration: falseAfterStruct: falseAfterUnion: falseAfterExternBlock: falseBeforeCatch: falseBeforeElse: falseIndentBraces: falseSplitEmptyFunction: trueSplitEmptyRecord: trueSplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Linux
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 120
CommentPragmas: '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
FixNamespaceComments: true
ForEachMacros:- foreach- Q_FOREACH- BOOST_FOREACH- list_for_each_entry
IncludeBlocks: Preserve
IncludeCategories:- Regex: '^"(llvm|llvm-c|clang|clang-c)/'Priority: 2SortPriority: 0- Regex: '^(<|"(gtest|gmock|isl|json)/)'Priority: 3SortPriority: 0- Regex: '.*'Priority: 1SortPriority: 0
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentCaseLabels: true
IndentGotoLabels: true
IndentPPDirectives: None
IndentWidth: 4
IndentWrappedFunctionNames: false
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 20
PenaltyBreakBeforeFirstCallParameter: 50
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 6000
PointerAlignment: Right
ReflowComments: true
SortIncludes: false
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
Standard: Latest
StatementMacros:- Q_UNUSED- QT_REQUIRE_VERSION
TabWidth: 8
UseCRLF: false
UseTab: Never
...