VS Code 變量參考

https://code.visualstudio.com/docs/editor/variables-reference

使用VS Code做C/C++開發時,需要配置3個文件:

  1. c_cpp_properties.json,指明include path,complier path等參數,供智能感知功能使用;
  2. tasks.json,指明編譯器及其運行參數等,用於啟動編譯構建;
  3. launch.json,指明調試器及其調試參數等,用於tasks環節生成的可執行文件的調試;

在這3個文件中,都有各種文件路徑需要配置,可以使用絕對路徑或文件名等,也可以使用變量來替代,這樣更靈活;VS Code的主要變量如下:

Variables Reference

Visual Studio Code supports variable substitution in Debugging and Task configuration files as well as some select settings. Variable substitution is supported inside key and value strings in launch.json and tasks.json files using ${variableName} syntax.

Predefined variables

The following predefined variables are supported:

  • ${workspaceFolder} - the path of the folder opened in VS Code
  • ${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)
  • ${file} - the current opened file
  • ${relativeFile} - the current opened file relative to workspaceFolder
  • ${relativeFileDirname} - the current opened file's dirname relative to workspaceFolder
  • ${fileBasename} - the current opened file's basename
  • ${fileBasenameNoExtension} - the current opened file's basename with no file extension
  • ${fileDirname} - the current opened file's dirname
  • ${fileExtname} - the current opened file's extension
  • ${cwd} - the task runner's current working directory on startup
  • ${lineNumber} - the current selected line number in the active file
  • ${selectedText} - the current selected text in the active file
  • ${execPath} - the path to the running VS Code executable
  • ${defaultBuildTask} - the name of the default build task

Predefined variables examples

Supposing that you have the following requirements:

  1. A file located at /home/your-username/your-project/folder/file.ext opened in your editor;
  2. The directory /home/your-username/your-project opened as your root workspace.

So you will have the following values for each variable:

  • ${workspaceFolder} - /home/your-username/your-project
  • ${workspaceFolderBasename} - your-project
  • ${file} - /home/your-username/your-project/folder/file.ext
  • ${relativeFile} - folder/file.ext
  • ${relativeFileDirname} - folder
  • ${fileBasename} - file.ext
  • ${fileBasenameNoExtension} - file
  • ${fileDirname} - /home/your-username/your-project/folder
  • ${fileExtname} - .ext
  • ${lineNumber} - line number of the cursor
  • ${selectedText} - text selected in your code editor
  • ${execPath} - location of Code.exe

Tip: Use IntelliSense inside string values for tasks.json and launch.json to get a full list of predefined variables.

進一步細節,可以深入研究https://code.visualstudio.com/docs/editor/variables-reference


分享到:


相關文章: