Skip to content
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

Open new terminal tab in same directory as existing tab (OSC 7?) #3158

Open
edencorbin opened this issue Oct 11, 2019 · 167 comments
Open

Open new terminal tab in same directory as existing tab (OSC 7?) #3158

edencorbin opened this issue Oct 11, 2019 · 167 comments
Labels
Area-Settings Issues related to settings and customizability, for console or terminal Area-VT Virtual Terminal sequence support Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. Product-Conpty For console issues specifically related to conpty Product-Powershell Issues in the modern command interpreter, Powershell.exe. Product-Terminal The new Windows Terminal.
Milestone

Comments

@edencorbin
Copy link

edencorbin commented Oct 11, 2019

Description of the new feature/enhancement

Have the option (or default) of a new terminal tab opening in the current directory of the window of which you hit the hotkey to open a new tab. This is the standard way most linux terminals work and is most handy. I often work in a directory where I need to launch multiple seperate processes, its a pain to CD back into the directory each time.

Proposed technical implementation details (optional)

Hit the new tab hotkey, the new terminal should then be in the same folder as the previouse.


maintainer edit: Before commenting, make sure to check out

Tutorial: Opening a tab or pane in the same directory in Windows Terminal

This is largely something configurable today, this issue is just tracking another way of configuring this

@edencorbin edencorbin added the Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. label Oct 11, 2019
@ghost ghost added Needs-Tag-Fix Doesn't match tag requirements Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Oct 11, 2019
@egmontkob
Copy link

There's a "standard" escape sequence (OSC 7 ; URI ST) to set the terminal emulator's belief about the current directory. It originates from macOS Terminal.app, and was later adopted by some other ones, including GNOME Terminal and as far as I know Konsole too.

Another possible approach is to do some OS-specific hacks to examine the inner state of the child process (or even further down to descendents).

Yet another possibility is to mix the two, e.g. go to the directory set via OSC 7 if it was ever emitted, otherwise dig into the process.

The advantage of the OSC 7 approach is that it's clever regarding when to and when not to follow a child process. E.g. you start another nested subshell, it'll be the subdirectory of that subshell that's taken into account because that one also emits this sequence. However, launch an app that internally changes directory (e.g. make) and it – luckily – won't be make's internal subdir that's used.

The disadvantage of OSC 7 is that it requires cooperation from the shell, or other apps that matter.

@DHowett-MSFT
Copy link
Contributor

I've been rejecting this feature request for as long as this project has been open-source, and I never learned about OSC 7. This is very exciting.

I'm not happy to crawl through the process tree to dig up the leafmost process's CWD, but I am extremely happy to support OSC 7.

@DHowett-MSFT DHowett-MSFT changed the title Open new terminal tab is same directory as previouse Open new terminal tab in same directory as existing tab Oct 14, 2019
@DHowett-MSFT DHowett-MSFT changed the title Open new terminal tab in same directory as existing tab Open new terminal tab in same directory as existing tab (OSC 7?) Oct 14, 2019
@DHowett-MSFT DHowett-MSFT added Area-Settings Issues related to settings and customizability, for console or terminal Area-VT Virtual Terminal sequence support Product-Conpty For console issues specifically related to conpty Product-Powershell Issues in the modern command interpreter, Powershell.exe. Product-Terminal The new Windows Terminal. and removed Needs-Triage It's a new issue that the core contributor team needs to triage at the next triage meeting labels Oct 23, 2019
@ghost ghost removed the Needs-Tag-Fix Doesn't match tag requirements label Oct 23, 2019
@davidhewitt
Copy link

FYI I asked about OSC 7 handling for Alacritty, which eventually led to this issue being created in the "Terminal WG" on gitlab: https://gitlab.freedesktop.org/terminal-wg/specifications/issues/20

There's not been much movement on that ticket, but you may be interested to follow it. Especially if you guys have any opinions regarding what a "formal" specification for OSC 7 might look like.

@zadjii-msft
Copy link
Member

So for the record, the thread at alacritty/alacritty#2937 has a pretty great discussion.

Honestly, I'm pretty okay using the de-facto standard that's already in place, the OSC 7 ; <URI> ST mechanism. I'm not really sure there needs to be anything more formal than that.


Wait no I had a terrible thought. Say bash is configured to emit that, and someone runs bash in WSL. What are we supposed to do when someone tries to set the working directory to /home/zadjii? How we:

  1. tell that this is a WSL path, not a Windows path
  2. know which WSL distro this came from?

Do we need to add some property on our side that indicates "this is a WSL distro, not a Windows exe"? What happens to users who don't set that, does the duplicate tab functionality just not work (effectively silently)?

Then the next part gets harder. What happens when this command is output over SSH? The terminal can't know that the path isn't on this machine anymore, right? How does Terminal.app handle this?

Maybe this does need more specification 😨

@egmontkob
Copy link

(Off: How long is it going to be until I mix up the two of you, D Howett and D Hewitt? :))

@slikts
Copy link

slikts commented Dec 10, 2019

This is a crucial missing feature.

@anthonyvdotbe
Copy link
Contributor

This should be a config option for the different commands. For example, I'd like this for duplicateTab and splitPane, but not for newTab.

@LuanVSO
Copy link
Contributor

LuanVSO commented Jan 26, 2020

the escape sequence is documented in mac os terminal.app preferences as shown by this comment alacritty/alacritty#2937(comment)

On macOS, the escape sequences are actually specified in Terminal.app > Preferences... > Profiles > Tab 69387948-67d69d00-0c95-11ea-881d-375672873fb4

@zadjii-msft
Copy link
Member

For the record, there's heated debate over in https://gitlab.freedesktop.org/terminal-wg/specifications/merge_requests/7 about the specification of exactly this feature. I doubt that we'll support any subset of this feature until there's an actually accepted proposal there - we'd rather not introduce another disparate implementation until there's an actual standard.

@egmontkob
Copy link

I'd recommend you to do the opposite :)

Quite a few terminals have successfully implemented OSC 7, copying from each other, resulting in happy users.

And there is somebody out there currently who thinks it isn't good enough, he thinks a formal specification is needed; comes up with a draft that is full of problems, and what I cannot understand at all, is only willing to document one of two siblings. (Note: I stopped following that thread a couple of days ago.)

Terminal-WG is not a formal authority, its documents aren't "official", aren't "standards" in any de jure way. It doesn't even have proper procedures, people with responsibilities, voting rights, whatever; no one knows what it takes to get a document "accepted" there, whatever this status means at all. It's just a collection of random unorganized folks trying to come up with something useful. Don't let those unofficial pending debates stop you from implementing a long-proven feature.

@LuanVSO
Copy link
Contributor

LuanVSO commented Jan 28, 2020

For the record, there's heated debate over in https://gitlab.freedesktop.org/terminal-wg/specifications/merge_requests/7

i commented it on the issue referenced in that pr too 😉

@zadjii-msft
Copy link
Member

2. Command line to use wsl.exe instead of Ubuntu.exe

Ah yes, that's something we need to work through with Canonical. Ubuntu.exe seemingly totally disregards the startingDirectory, which helps power this.

@dotnetCarpenter
Copy link
Contributor

dotnetCarpenter commented May 18, 2022

Dang! For some reason the solution from https://docs.microsoft.com/en-us/windows/terminal/tutorials/new-tab-same-directory stopped working in Windows Terminal 1.13.10983.0 (or before). It's working for window splitting (panes) but not for new tabs.

Using the Ubuntu image.

@TBBle
Copy link

TBBle commented May 19, 2022

Are you using "new tab" or "duplicate tab". The former doesn't carry the working directory over.

@tristanbarcelon
Copy link

Are you using "new tab" or "duplicate tab". The former doesn't carry the working directory over.

I'm using new tab. Turns out the profile needs to properly escape single slash on Server 2022, something I didn't have to do in Windows 10/11. Fix mentioned by nuixtech fixed the issue. I have terminal installed on Windows 11 and Server 2022 (using the win10 msixbundle). This is how Ubuntu-20.04 profile looks on Windows 11.

image

On Server 2022, this is how it looks.

image

@TBBle
Copy link

TBBle commented May 27, 2022

I don't understand how that makes a difference (because that path should be processed by Terminal, well before WSL sees it), but it might be worth opening as a separate specific bug report so it can be fixed in the code that auto-generates the profile, or if there's a bug elsewhere in Terminal causing this issue, that can be fixed instead.

@codtiger
Copy link

Are there any updates on this ? I managed to make it work for "duplicate tab" by changing ubuntu.exe to wsl.exe -d Ubuntu in the profile section but it still does not work on new tab:(

@InvisibleProgrammer
Copy link

@codtiger , thank you for your answer. It saved me :D

One additional detail: with default settings, when I change ubuntu.exe to wsl.exe -d Ubuntu, the starting directory becomes the /mnt/c/WINDOWS/system32. To fix that, I've changed starting directory to ~:

                "startingDirectory": "~",

@nileshgr
Copy link

@codtiger @tristanbarcelon was irritated by this problem, thanks for the suggestions! 😄

@alkorlos
Copy link

alkorlos commented May 27, 2023

Good day!

I am using:
Windows 10 22H2
Windows Terminal 1.16.10261.0
Ubuntu 22.04.2 LTS

Following this instruction Tutorial: Opening a tab or pane in the same directory in Windows Terminal added to .bash_profile line

PROMPT_COMMAND=${PROMPT_COMMAND:+"$PROMPT_COMMAND; "}'printf "\e]9;9;%s\e\\" "$(wslpath -w "$PWD")"'

After that, Windows Terminal began to remember directories, thanks!

But other problems arose, for example, nvm broke
Command 'nvm' not found

Adding these lines to .bash_profile helped fix this problem

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

I decided to write this comment because neither in the article Tutorial: Opening a tab or pane in the same directory in Windows Terminal nor in this issue did I see that adding this line

PROMPT_COMMAND=${PROMPT_COMMAND:+"$PROMPT_COMMAND; "}'printf "\e]9;9;%s\e\\" "$(wslpath -w "$PWD")"'

to .bash_profile may cause additional problems.

nvm is the first thing I tried to use, most likely there are more such problems. Did I do something wrong when I edited .bash_profile? Or is it known that adding this line breaks paths or something like that?

@TBBle
Copy link

TBBle commented May 27, 2023

That line definitely shouldn't have broken stuff you described. It's not obvious how nvm was working, but my best-guess is that you actually newly created the .bash_profile, and now bash is running that instead of some other file, like .profile, and your existing NVM setup was in that. This site suggests that this is the case, but I haven't looked myself. This answer suggests that maybe it's .bashrc that is no longer being read, but that's older than Ubuntu 22.04.

If that's the case, then you could either add the PROMPT_COMMAND to .profile (or whatever it is) and delete the newly-created .bash_profile, or add something like source ~/.profile to the start of your .bash_profile.

@alkorlos
Copy link

alkorlos commented May 27, 2023

Thank you!

Important clarification — I did not do anything unusual, on the contrary, yesterday I performed a clean installation of Ubuntu 22.04.2 LTS.
After a clean install, I installed only nvm. After that of the files listed here: .bash_profile, .bash_login, .profile, only exists .profile. File .bash_profile I created, as it was written here. In addition to the problem with nvm, there was a problem with colors, but I did not betray this.

Indeed, adding PROMPT_COMMAND to the .profile and deleting .bash_profile helped.

Thanks again! Cheers!

@TBBle
Copy link

TBBle commented May 28, 2023

Okay, so it might make sense to add a note to the docs along the lines of "If .bash_profile does not exist, but .profile does, add the line there instead", to avoid confusion for the default Ubuntu 22.04 setup and any similar situations.

The current instructions do not say to create the file, but in this situation, it's a reasonable guess that happens to do the wrong thing.

I note the MINGW instructions use .bashrc instead, which won't have this problem. That's maybe a better choice, as it's specifically for interactive shells, IIUC.

@daviewales
Copy link

The creator of powerlevel10k has created a Zsh plugin for this.
He claims it "...achieves the same thing as the code snippet in the official Windows Terminal documentation but with fewer bugs and better performance".

Linking it here for visibility:
https://github.com/romkatv/windows-terminal-zsh-integration

@TBBle
Copy link

TBBle commented Jun 9, 2023

I don't know the zsh ecosystem, but is that plugin widely-suitable-enough to be added as a preferred option to the Windows Terminal docs?

It's clearly too-long to inline into the docs (and is doing a lot more than the one-liner does, such as caching, some TMUX-specific magic and for some reason using wslpath -am and mangling the output rather than wslpath -w), but if general zsh users can do an extra git clone over the current docs and get a much better result, then it seems like an easy win to me.

If zsh plugin managers are commonly used, then that's even better, IMHO.

The main concern I envisage is making the docs dependent on an external site.

@microsoft microsoft deleted a comment from kudorgyozo Jun 15, 2023
@Asterikss
Copy link

In regard to comments made by alkorlos and TBBle.
This might help somebody in the future:

  • TL;DR:
    • remove potential "cd ~" or "cd ~/my-preferred-starting-dir" from .bashrc
    • remove .bash_profile if using .profile (then move PROMPT_COMMAND to .profile or .bashrc)
  1. On a few forums on the Internet it is advised to put "cd ~" or some variation of this in .bashrc to change the starting directory. This can be overlooked and will obviously make PROMPT_COMMAND not produce the desired result.

  2. If you did not have .bash_profile and you created it, as it was written here, PROMPT_COMMAND will work, given you put it in .bash_profile, however this will also "skip" your "config" (.bashrc and .profile). .bashrc is not "loaded" by default. .profile is responsible for that, but it is also not always "loaded".

First few lines of .profile:

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.

Solutions:

  • copy the contents of .profile to .bash_profile. e.g.
...
# if running bash
if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
	. "$HOME/.bashrc"
    fi
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/.local/bin" ] ; then
    PATH="$HOME/.local/bin:$PATH"
fi
...

or

  • remove .bash_profile and put PROMPT_COMMAND in either .profile or .bashrc

@deadcoder0904
Copy link

why is this not implemented even 5 years later?

i don't think this is unreasonable for any web developer working on a project that requires to spin up >2 scripts in the same directory.

@zadjii-msft
Copy link
Member

Well, I mean, it has and it hasn't.

You've been able to use shell integration to open a new tab/pane in the same CWD for a couple years now, using OSC9;9. (osc9;9 was originally tracked in #8166) This thread is technically tracking a similar escape sequence, OSC7, which we haven't had a chance to come back around on yet (because it's got some trickier edge cases with WSL distros). #8214 (linked earlier in this thread) has some of the most complete discussion on the whole problem space.

Alas, automagic CWD detection (like linux) will almost certainly never work, as some shells (cough powershell) don't actually set their CWD.

Then there's of course the potential for automatically modifying people's prompts, such that shell integration is automatically enabled for them. This is definitely something that I want to investigate going forward, maybe sometime next year. But I doubt that'll work for everyone or every shell.

@ewlondon
Copy link

ewlondon commented Feb 10, 2024

I know this is an old topic but I just wanted to say for anyone trying to get split panes to same directory using GitBash in 2024, adding this line to .bashrc finally worked for me. The previously listed command by JahzVH was not working correctly for me and would give an error ping every time a prompt was input into terminal.

PROMPT_COMMAND=${PROMPT_COMMAND:+"$PROMPT_COMMAND "}'printf "\e]9;9;%s\e\\" "$(cygpath -w "$PWD")"'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Settings Issues related to settings and customizability, for console or terminal Area-VT Virtual Terminal sequence support Issue-Feature Complex enough to require an in depth planning process and actual budgeted, scheduled work. Product-Conpty For console issues specifically related to conpty Product-Powershell Issues in the modern command interpreter, Powershell.exe. Product-Terminal The new Windows Terminal.
Projects
None yet
Development

Successfully merging a pull request may close this issue.