Skip to content

Shortcut to change the current directory in the PowerShell console to the currently opened file? #2189

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
skataben opened this issue Sep 18, 2019 · 5 comments

Comments

@skataben
Copy link

I am running the PowerShell extension in Visual Studio Code with its PowerShell Integrated Console attached. I have two PowerShell scripts each open in separate tabs. The files are located in different directories.

I would like to quickly change the current directory to the currently active tab in the PowerShell console using a keyboard shortcut (e.g. Ctrl+Alt+D).

How does one accomplish this?

https://stackoverflow.com/questions/57893791

@ghost ghost added the Needs: Triage Maintainer attention needed! label Sep 18, 2019
@TylerLeonhardt
Copy link
Member

TylerLeonhardt commented Sep 18, 2019

(NOTE: All of this code must be run in the PowerShell Integrated Console)

You would do it like this in script:

Set-Location ([IO.Path]::GetDirectoryName($PSEditor.GetEditorContext().CurrentFile.Path))

You could register an Editor Command to be quick about it:

Register-EditorCommand -Name ChangeToDir -DisplayName "Change to the Directory of the Current File" -ScriptBlock {
    Set-Location ([IO.Path]::GetDirectoryName($PSEditor.GetEditorContext().CurrentFile.Path))
}

Then you can do:

cmd+shift+s (or ctrl+shift+s on non-macOS I think)

Then find "Change to the Directory of the Current File" in the list!

@code-glue
Copy link

Thank you for the response. It works.

Is there no way to assign a keyboard shortcut directly to "ChangeToDir"?

@SeeminglyScience
Copy link
Collaborator

Not currently due to #2145, but after that's fixed it'll be possible.

@skataben
Copy link
Author

Thank you

@skataben
Copy link
Author

skataben commented Sep 19, 2019

I found a better solution from StackOverflow that works in both the cmd and PowerShell consoles. (Better because it is a keyboard shortcut and not a menu selection.)

In the keybindings.json file, add the following key binding:

{
"key": "ctrl+alt+d",
"command": "workbench.action.terminal.sendSequence",
"args": {"text": "cd \"${fileDirname}\"\u000D"}
}

https://stackoverflow.com/questions/57893791

@SydneyhSmith SydneyhSmith removed the Needs: Triage Maintainer attention needed! label Sep 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants