VS Code终端中是否有当前文件或文件夹的变量?

时间:2017-04-24 15:53:14

标签: powershell visual-studio-code

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

ii $vsActivePath

ii $vsActiveFile

这可能吗?

3 个答案:

答案 0 :(得分:2)

不确定这是否正是您正在寻找的内容,但您可以访问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

答案 1 :(得分:1)

他们添加了一些新变量,它包含${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

答案 2 :(得分:0)

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

在 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