简介
VS Code Debuging 配置文件 、Task 配置文件和一些可选设置中都支持变量替换,VS Code 中支持变量替换的变量以 key:value
形式定义,且 key 和 value 都是字符串。在 launch.json 和 task.json 中经常使用到变量替换,变量使用的语法为: ${variableName} 。
预定义变量
VS Code 预定义了如下变量:
假设在VS Code 编辑器中打开了文件 /home/your-username/your-project/folder/file.ext
, 目录 /home/your-username/your-project
在 VS Code 的根目录中打开。在此情况下,预定义各变量的值如下:
- ${userHome}:系统当前用户家目录
变量值 : /home/your-username
- ${workspaceFolder}:VS Code 当前打开的文件夹路径
变量值: /home/your-username/your-project
- ${workspaceFolderBasename}:VS Code 当前打开的文件夹名称
变量值: your-project
- ${file}: VS Code 当前打开的文件路径
变量值: /home/your-username/your-project/folder/file.ext
- ${fileWorkspaceFolder}: VS Code 当前打开的文件夹绝对路径
变量值: /home/your-username/your-project
- {workspaceFolder} ,编辑器中当前打开文件的相对路径
变量值: folder/file.ext
- {workspaceFolder},编辑器中当前打开文件所在文件夹的名称
变量值: folder
- ${fileBasename}:编辑中打开的当前文件名,含后缀
变量值: file.ext
- ${fileBasenameNoExtension}:编辑中打开的当前文件名,不含后缀
变量值: file
- ${fileDirname}:编辑器中当前打开文件所在文件夹的绝对路径
变量值: /home/your-username/your-project/folder
- ${fileExtname}:编辑器中当前打开文件的后缀
变量值: .ext
- ${cwd}:the task runner's current working directory upon the startup of VS Code
变量值:-
- ${lineNumber}:the current selected line number in the active file
变量值: -
- ${selectedText}: 编辑器打开且在激活状态文件中当前选择的文字
变量值: 当前选中的文字
- ${execPath}:Code.exe/code 可执行程序的绝对路径
变量值: Code.exe/code 可执行程序的绝对路径(location of Code.exe)
- ${defaultBuildTask}:the name of the default build task
变量值:the name of the default build task
- ${pathSeparator}:the character used by the operating system to separate components in file paths
变量值: / on macOS or linux, \ on Windows
Tip: 在 tasks.json
和 launch.json
中使用预定义变量,VS Code 智能感知(IntelliSense)将动态提供一个预定义变量列表。