1. 程式人生 > 程式設計 >淺析VSCode tasks.json中的各種替換變數的意思 ${workspaceFolder} ${file} ${fileBasename} ${fileDirname}等

淺析VSCode tasks.json中的各種替換變數的意思 ${workspaceFolder} ${file} ${fileBasename} ${fileDirname}等

When authoring tasks configurations,it is often useful to have a set of predefined common variables. VS Code supports variable substitution inside strings in the tasks.json file and has the following predefined variables:

  • ${workspaceFolder} the path of the workspace folder that contains the tasks.json file
  • ${workspaceRootFolderName} the name of the folder opened in VS Code without any slashes (/)
  • ${file} the current opened file
  • ${relativeFile} the current opened file relative to the workspace folder containing the file
  • ${fileBasename} the current opened file's basename
  • ${fileBasenameNoExtension} the current opened file's basename without the 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

You can also reference environment variables through ${env:Name} (for example,${env:PATH}). Be sure to match the environment variable name's casing,for example ${env:Path} on Windows.

Below is an example of a custom task configuration that passes the current opened file to the TypeScript compiler.

{
  "taskName": "TypeScript compile","type": "shell","command": "tsc ${file}","problemMatcher": [
    "$tsc"
  ]
}

部分翻譯:(來自網際網路)

${workspaceRoot} 當前開啟的資料夾的絕對路徑+資料夾的名字

${workspaceRootFolderName} 當前開啟的資料夾的名字

${file}當前開啟正在編輯的檔名,包括絕對路徑,檔名,檔案字尾名

${relativeFile}從當前開啟的資料夾到當前開啟的檔案的路徑

如 當前開啟的是test資料夾,當前的開啟的是main.c,並有test / first / second / main.c

那麼此變數代表的是 first / second / main.c

${fileBasename} 當前開啟的檔名+字尾名,不包括路徑

${fileBasenameNoExtension} 當前開啟的檔案的檔名,不包括路徑和字尾名

${fileDirname} 當前開啟的檔案所在的絕對路徑,不包括檔名

${fileExtname} 當前開啟的檔案的字尾名

${cwd} the task runner's current working directory on startup

不知道怎麼描述,這是原文解釋,

跟 cmd 裡面的 cwd 是一樣的

${lineNumber} 當前開啟的檔案,游標所在的行數

更新一個連結:https://code.visualstudio.com/docs/editor/variables-reference

總結

到此這篇關於淺析VSCode tasks.json中的各種替換變數的意思 ${workspaceFolder} ${file} ${fileBasename} ${fileDirname}等的文章就介紹到這了,更多相關VSCode tasks.json 替換變數內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!