Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quickly change shell folder to match the currently open file

With single file opened in vscode, when I open integrated Powershell it always starts in my $HOME folder.

Is there any way to quickly switch to the directory of the currently shown file without having to manually cd into it?

Something like cd $vsCurrentFileDirectory.

Currently, I right click the tab and copy path then cd (Split-Path <CTRL-v>).

like image 604
majkinetor Avatar asked May 20 '19 10:05

majkinetor


People also ask

How do you change directory in terminal or code?

Open Visual Studio Code and access the Command Palette (⇧⌘P) and start typing shell command and select option Shell Command: Install 'code' command in PATH. After that you're able to start a new terminal window, change into your project directory and use code . to open the current directory in Visual Studio Code.

What is shell folder in Windows 10?

Shell folder in windows. Shell folders in Windows are specific folders that are used as the default location for specific file types. Press Window + R, then type Shell: folder shortcut, then press enter. Example:- To open the windows installation folder.

How to change shells in Linux?

All the shells available on your Linux systems are listed in the file /etc/shells. You can use cat command or less command to view the content of the file. How to change the shell to use another one? If you want to use a different shell, you can simply type its name and you’ll be logged into the new shell.

How to open windows installation folder using shell folder shortcut?

Registry Configuration :- HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders Press Window + R, then type Shell: folder shortcut , then press enter. Example:- To open the windows installation folder.

How do I open multiple files with the same file type?

In the Info window for your file, click to open the Open With subcategory. From here, you can select a new application to open files with the same file type. Select an alternative app from the menu, then press the Change All button to apply the change to all files with the same file type.


1 Answers

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

which does indeed work in the Insider's Build. This will create a new terminal though, not modify an existing terminal.


[Original Answer]: This will change the current 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"}
},

The \u000D is a return so the command triggers immediately.

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.


Suggested edit to be tested :

Note that on windows, you must use the following instead:

"args": {"text": "cd /d \"${fileDirname}\"\u000D"}

This is because on Windows, the /d parameter must be used with cd to switch drives.


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

with discussion of the /d flag. Thanks to @skataben for the additional info.


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

But my first sendSequence keybinding remains the fastest way to do this.

like image 166
Mark Avatar answered Oct 03 '22 22:10

Mark