Visual Studio Code中当前活动文件所在文件夹的完整路径

时间:2016-04-20 05:58:12

标签: visual-studio-code

我想在Visual Studio Code中创建一个任务,但我需要一个指向实际文件的路径。有选择吗?

我的任务:

{
    "version": "0.1.0",
    "command": "${workspaceRoot}/run.sh",
    "isShellCommand": true,
    "options": {
        "cwd": "${hereINeedPathToActualFile}"
    },
    "args": ["${file}"],
    "showOutput": "always"
}

4 个答案:

答案 0 :(得分:12)

window.title是用户设置中适合我的设置:
"window.title": "${activeEditorMedium}"

其他选择:

  // Controls the window title based on the active editor. Variables are substituted based on the context:
  // ${activeEditorShort}: e.g. myFile.txt
  // ${activeEditorMedium}: e.g. myFolder/myFile.txt
  // ${activeEditorLong}: e.g. /Users/Development/myProject/myFolder/myFile.txt
  // ${rootName}: e.g. myProject
  // ${rootPath}: e.g. /Users/Development/myProject
  // ${appName}: e.g. VS Code
  // ${dirty}: a dirty indicator if the active editor is dirty
  // ${separator}: a conditional separator (" - ") that only shows when surrounded by variables with values
  "window.title": "${activeEditorShort}${separator}${rootName}",

答案 1 :(得分:4)

如果您需要访问文件,可以从工作区根目录中获取其位置:

"filelocation": "${workspaceRoot}/.vscode/tasks.json",

// Available variables which can be used inside of strings.
// ${workspaceRoot}: the root folder of the team
// ${file}: the current opened file
// ${fileBasename}: the current opened file's basename
// ${fileDirname}: the current opened file's dirname
// ${fileExtname}: the current opened file's extension
// ${cwd}: the current working directory of the spawned process

答案 2 :(得分:4)

转到设置,在UserSettings中将此行添加到json blob:

  

“window.title”:“$ {activeEditorLong}”

答案 3 :(得分:1)

我在“launch.json”文件中使用以下内容在启动Python程序之前设置当前工作目录:

    {
        "name": "Python: Current File",
        "type": "python",
        "request": "launch",
        "program": "${file}",
        "cwd": "${fileDirname}",
        "stopOnEntry": false,
        "console": "integratedTerminal"
    },

因此实际文件目录的路径在$ {fileDirname}中。 我不使用$ {workspaceRoot},因为它已被弃用,它是工作空间根目录的路径,而不是cwd,如果cwd是根目录以外的文件夹。

这里有一个所有Visual Studio Code Task变量的列表: https://code.visualstudio.com/docs/editor/variables-reference

相关问题