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

问题描述 投票:5回答:5

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

我的任务:

{
    "version": "0.1.0",
    "command": "${workspaceRoot}/run.sh",
    "isShellCommand": true,
    "options": {
        "cwd": "${hereINeedPathToActualFile}"
    },
    "args": ["${file}"],
    "showOutput": "always"
}
visual-studio-code
5个回答
13
投票

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}",

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

4
投票

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

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


4
投票

这几个月前已经解决了这个问题:

https://github.com/Microsoft/vscode/issues/3119

有一个新设置window.showFullPath,一旦启用,将显示当前打开的文件的完整路径,而不是工作区相对路径。

该功能计划于11月发布,目前正在测试中。然后你可以在配置文件中使用window.showFullPath来控制它。


更新:

自我发布原始答案后,设置已更改。它现在称为window.title,您可以自定义任何您喜欢的内容。


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

© www.soinside.com 2019 - 2024. All rights reserved.