如何快速更改shell文件夹以匹配当前打开的文件 [英] How to quickly change shell folder to match the currently open file
问题描述
在 vscode 中打开单个文件,当我打开集成的 Powershell 时,它总是在我的 $HOME
文件夹中启动.
With single file opened in vscode, when I open integrated Powershell it always starts in my $HOME
folder.
有没有什么办法可以快速切换到当前显示文件所在的目录,而无需手动cd
进去?
Is there any way to quickly switch to the directory of the currently shown file without having to manually cd
into it?
类似于cd $vsCurrentFileDirectory
.
目前,我右键单击选项卡并复制路径,然后cd (Split-Path <CTRL-v>)
.
Currently, I right click the tab and copy path then cd (Split-Path <CTRL-v>)
.
推荐答案
EDIT:将在 v1.39 中添加一个新命令以使其更简单,请参阅发行说明.给出的示例键绑定是:
EDIT: A new command will be added in v1.39 to make this more straightforward, see release notes. The example keybinding given is:
{
"key": "cmd+shift+h",
"command": "workbench.action.terminal.newWithCwd",
"args": {
"cwd": "${fileDirname}"
}
}
这确实适用于 Insider's Build.不过,这将创建一个新终端,而不是修改现有终端.
which does indeed work in the Insider's Build. This will create a new terminal though, not modify an existing terminal.
[原始答案]:这将改变当前终端.
您可以设置一个键绑定来很容易地做到这一点:
You can set up a keybinding to do this pretty easily:
{
"key": "alt+t",
"command": "workbench.action.terminal.sendSequence",
"args": {"text": "cd '${fileDirname}'\u000D"}
},
\u000D
是一个返回,所以命令会立即触发.
The \u000D
is a return so the command triggers immediately.
另请注意,我将 '${fileDirname}'
放在引号中,以防您的目录名称中包含空格.
Also note that I put the '${fileDirname}'
in quotes in case your directory name has spaces in it.
无论焦点在终端还是文件中,键绑定都将起作用.
The keybinding will work whether focus is in the terminal or the file.
建议进行测试的
请注意,在 Windows 上,您必须使用以下代码:
"args": {"text": "cd /d \"${fileDirname}\"\u000D"}
这是因为在 Windows 上,/d
参数必须与 cd
一起使用才能切换驱动器.
This is because on Windows, the /d
parameter must be used with cd
to switch drives.
另见快捷方式更改 Powershell 和 cmd 中的目录以获取更多信息,以防您在 Powershell 中更改驱动器号和转义双引号.
Also see shortcut to change directory in Powershell and cmd for additional info in case you are changing drive letters and escaping double quotes in Powershell.
{
"key": "ctrl+alt+d",
"command": "workbench.action.terminal.sendSequence",
"args": {"text": "cd \"${fileDirname}\"\u000D"}
}
讨论了 /d
标志.感谢@skataben 提供更多信息.
with discussion of the /d
flag. Thanks to @skataben for the additional info.
或者,有一个扩展可以做到这一点:terminal-here,但键绑定实际上工作得更快.创建该扩展时,sendSequence
和变量替换功能不可用.
Alternatively, there is an extension to do this: terminal-here, but the keybinding actually works faster. The sendSequence
and variable substitution functionality was not available when that extension was created.
最后,如果您在资源管理器中右键单击某个文件夹,则会在其中出现一个 在终端中打开
选项(以及相应的命令).这意味着您可以像这样在键绑定中使用该命令:
Finally, if you right-click on a folder in the explorer, there is an Open in Terminal
option there (and corresponding command). Which means you could use that command in a keybinding like so:
{
"key": "alt+t",
"command": "openInTerminal"
}
但我的第一个 sendSequence
键绑定仍然是最快的方法.
But my first sendSequence
keybinding remains the fastest way to do this.
这篇关于如何快速更改shell文件夹以匹配当前打开的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!