【问题标题】:Are there variables for current file or folder in the VS Code terminal?VS Code 终端中的当前文件或文件夹是否有变量?
【发布时间】:2017-09-21 09:41:12
【问题描述】:

在终端窗格中,我想通过环境变量或 PowerShell 变量(首选)引用活动编辑器选项卡的文件或文件夹。像这样的:

ii $vsActivePath

ii $vsActiveFile

这可能吗?

【问题讨论】:

  • 您所说的是使用$pseditor,它是集成终端中为 PS 扩展提供的变量。目前可用的不多,related SO question on same topic
  • 您链接的 SO 问题将我引导至 PowerShellEditorServices。由于还没有模块形式,因此需要做一些工作,但这是有希望的。

标签: powershell visual-studio-code


【解决方案1】:

不确定这是否正是您要查找的内容,但您可以访问 vs 代码中的各种变量,例如:

${workspaceRoot} the path of the folder opened in VS Code
${workspaceRootFolderName} the name of the folder opened in VS Code without any slashes (/)
${file} the current opened file

Full details can be found here

【讨论】:

  • 如何在终端访问?
【解决方案2】:

他们添加了一些新变量,其中包含${fileDirname},这是您当前${file} 所在目录的路径。 这里的文档似乎有点误导。

这些是当前可用的选项:

${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
${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

【讨论】:

  • 这些用于 conf 文件中的变量替换,但它们在终端中似乎不可用。
【解决方案3】:

我找到的最接近的是:您可以设置一个键绑定,将文本发送到终端。

在 keybindings.json 中,添加如下条目:

{
    "key": "ctrl+shift+f",
    "command": "workbench.action.terminal.sendSequence",
    "args": { "text": "${relativeFile}" },
    "when": "terminalFocus"
},

如您所见,变量替换有效,因此您可以插入文件、相对文件、路径等(如Sn0wfreeze's answer 中所述)

另外请注意,我在示例中使用了“when”子句,以便仅当终端是活动焦点时才礼貌地覆盖现有快捷方式。这是可选的。

我认为这明显不如使用内联变量好,但对于我的日常工作来说已经足够了。您可能会喜欢使用顺序快捷方式来提供很多选项,例如:

ctrl+f, ctrl+f => ${file}
ctrl+f, ctrl+r => ${relativeFile}
ctrl+f, ctrl+d => ${fileDirname}
etc.

Original source on visualstudio.com

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-30
    • 2022-10-03
    • 1970-01-01
    • 2022-08-11
    相关资源
    最近更新 更多