31

I want to simply start a new tab in the same folder as my previous tab that I was in. Is this possible, and if so how? This is with the Windows Terminal by the way.

Now if I open a new tab, it will open in home directory (which is configured in the Windows Terminal's settings page).

HKTonyLee
  • 3,111
  • 23
  • 34
DJ Poland
  • 955
  • 1
  • 9
  • 23
  • 7
    Looks like this is now supported. CTRL+SHIFT+D duplicates the current tab. Also see Settings > Actions for the list of key bindings. – YRH Jun 17 '21 at 09:22
  • 3
    Can't post an answer now since it's closed but check out https://learn.microsoft.com/en-us/windows/terminal/tutorials/new-tab-same-directory – Warpstar22 Jan 19 '22 at 17:10
  • 1
    @YRH has already said what I was going to say here! But I also want to add that **recently a new option has been added in the Terminal app settings exactly for this very purpose.** Enable it this way: **Settings -> Desired Profile (e.g., Powershell, or Pwsh, or etc.) -> Starting directory -> check the checkbox option "Use parent process directory"**. – aderchox May 13 '23 at 21:34

8 Answers8

13
  1. Open the windows terminal
  2. Click the dropdown button
  3. Click the settings option
  4. set the startingDirectory value to "./"
Example
"profiles":
    {
        "defaults":
        {
            // Put settings here that you want to apply to all profiles.
            "startingDirectory": "./" <---- set this value
        }

George C.
  • 6,574
  • 12
  • 55
  • 80
  • 1
    While this piece of code may provide a solution to the question, it's better to add context as to why/how it works. This can help future users refer to and eventually apply this knowledge to their own code. You are also likely to have positive feedback/upvotes from users, when the code is explained. – Amit Verma Mar 03 '21 at 07:27
  • 1
    I tried this in the Windows Terminal and it didn't work. I am using Ubuntu with WSL2 and it didn't exited on startup of a new tab (with Ubuntu) with an error. – Lakshya Goyal Sep 25 '22 at 00:06
  • 4
    This didn't work for me on PowerShell, it sets the starting directory to System32 – Lotfi Oct 02 '22 at 09:43
  • 1
    This solution, doesn't work. -1 – tantra35 Jun 13 '23 at 14:16
  • 1
    Real solution described here https://learn.microsoft.com/en-us/windows/terminal/tutorials/new-tab-same-directory – tantra35 Jun 13 '23 at 14:23
10

The Windows Terminal command line interface (wt.exe) can be used to achieve this, but with a command instead of a keyboard shortcut.

wt -w 0 nt -d .
  • -w 0: Use the current terminal window.
  • nt or new-tab: Create a new tab.
    • -d .: Start the process in the current directory.

Doc: https://learn.microsoft.com/windows/terminal/command-line-arguments

ravindUwU
  • 657
  • 11
  • 26
10

Zsh

If you are using Zsh, you can add this to ~/.zshrc

[[ -n "$WT_SESSION" ]] && {
  chpwd() {
    echo -en '\e]9;9;"'
    wslpath -w "$PWD" | tr -d '\n'
    echo -en '"\x07'
  }
}

After that, you can use duplicate tab in Windows Terminal to open the same directory in new tab


Bash

You can also do similar thing in Bash

my_prompt() {
  echo -en '\e]9;9;"'
  wslpath -w "$PWD" | tr -d '\n'
  echo -en '"\x07'
}

export PROMPT_COMMAND=my_prompt

Explanation

$WT_SESSION is an environment set by Windows Terminal. This will ensure we define this escape sequence only in WT.

chpwd() is a callback function that Zsh will invoke whenever it changes the working directory.

Inside the chpwd() it will send a OSC 9;9 escape sequence. This will tell the terminal (many terminals including Windows Terminal) that the CWD of the shell has been changed.

The syntax of OSC 9;9 escape sequence is \e]9;9;"C:\Temp\"\x07

The C:\Temp\ in the sequence has to be Windows-style path. It can point to some path in WSL (e.g. \\wsl.localhost\Ubuntu-20.04\home\superman\bin). When opening new tab, the shells will open corresponding UNIX path (e.g. /home/superman/bin).

HKTonyLee
  • 3,111
  • 23
  • 34
  • 3
    This is currently the only answer, that currently fixes this issue. It is furthermore very well explained. – jacktorrancee Mar 11 '22 at 10:48
  • 1
    for fish shell this can be achieved with: function storePathForWindowsTerminal --on-variable PWD if test -n "$WT_SESSION" printf "\e]9;9;%s\e\\" (wslpath -w "$PWD") end end – kDar Mar 16 '22 at 16:19
7

This is not currently possible, as explained here https://github.com/microsoft/terminal/issues/1437 the issue is closed at the moment but could perhaps be re-opened in future.

LeaveTheCapital
  • 2,530
  • 1
  • 7
  • 11
  • 2
    update - this is now being tracked under this issue, the spec hasn't been agreed upon at time of writing https://github.com/microsoft/terminal/issues/3158 – LeaveTheCapital Jun 02 '20 at 07:35
  • 2
    this is possible right now using `OSC 9;9` sequence. please check the answer below to see how to do that – HKTonyLee Oct 30 '21 at 19:35
4

I've found something interesting.... This worked for me... You can put:

"startingDirectory": "./"

In the terminal configuration....

Example

{
    // Make changes here to the powershell.exe profile.
    "guid": "{61c54bbd-c2c6-5271-96e7-009a87ff44bf}",
    "name": "Windows PowerShell",
    "commandline": "powershell.exe",
    "startingDirectory": "./",
    "hidden": false
},
joshhunt
  • 5,197
  • 4
  • 37
  • 60
gdonega
  • 65
  • 1
  • 1
  • 1
    Welcome to Stackoverflow. Answers should not be screenshots, please post code in a codeblock. Also this answer shows how to to open a tab the current directory not the cuurent directory of previous tab as the user asks. – apena Aug 13 '20 at 15:37
  • 3
    Unfortunately this only lets you determine the starting directory, it doesn't copy the previous directory when opening a new tab. – joshhunt Dec 28 '20 at 07:58
1

You can always rewrite setting json evertime you open a tab but its a shell specific hack.

Put this function into $PROFILE (make sure to adjust $path)

function sd {
  $path = 'C:\Users\Admin\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json'
   ((Get-Content -path $path) -replace '"startingDirectory":.*', ("`"startingDirectory`": `"$pwd`"") -replace "\\", "\\") | Set-Content -Path $path
}

Solution taken from here.

apena
  • 2,091
  • 12
  • 19
1
dotnet tool install -g p2u
p2u dir ${PWD}

Then CTRL+SHIFT+D
Hopefully it will work with split pane as well

Same idea of @apena but now with a dotnet tool. For further details: https://github.com/celsojr/p2u

Celso Jr
  • 136
  • 10
1

Also a similar vein as @apena's post as it is specific to bash.

In my case I wanted Debian to open on my current working directory.

Although I couldn't exactly get this behavior I was able to get it to open the last directory visited (effectively) using a few hacks:

In my linux distro, I added this to my .bashrc to intercept all calls to cd and store the last call to cd into a destination of my choice.

cd() {
  builtin cd "$@" ;  echo $PWD > /opt/chdir/lastestdir
}

Next, since Windows Terminal didn't let me inline the command, I created this batch script to in a custom location:

@echo off
for /F "tokens=*" %%n IN (\\wsl$\Debian\opt\chdir\lastestdir) DO @(wsl.exe -d Debian --cd \\wsl$\Debian%%n)

NOTE: \\wsl$\Debian is the path to my distro within Powershell, and the \opt\chdir\lastestdir is based on the path I used in the previous step.

Next, I edited the profile for Debian in Windows terminal to point at my script: WT Debian profile

I'm open for suggestions, but this let me happily duplicate my current directory to the next tab as long as it was the last terminal I had changed directory in.

SGM1
  • 968
  • 2
  • 12
  • 23