Skip to content

New terminal opens always in home directory when called from Tilix #2649

Closed

Description

@zbraniecki

Describe the bug
When a terminal is open to some directory in Tilix and I open a new one, bash and zsh will open it in the same directory as the previous terminal, but after setting Nushell as the default it always opens in home directory.

To Reproduce
Steps to reproduce the behavior:

  1. Install tilix
  2. Set nushell as the default shell with chsh
  3. Open terminal in tilix
  4. Navigate to some directory
  5. Call New Terminal (ctrl+shift+a in my case)

Expected behavior
New terminal opens in the same directory as the previous one.

Configuration (please complete the following information):

  • OS (e.g. Windows): Ubuntu 20.04
  • Nu version (you can use the version command to find out): 0.20

Activity

  1. fdncred commented on Oct 8, 2020

    @fdncred
    Contributor

    It looks like tilix must be passing the path to the terminal but I'm not sure exactly how it works under the hood.

  2. zbraniecki commented on Oct 21, 2020

    @zbraniecki
    Author

    It seems like tilix is just calling PWD=~/dir to set the current working dir and bash/zsh accept it while NuShell does not.

    Gnome Terminal seems to be using some other strategy because it works with Nushell (a'ka, it opens new tab in the current dir). Can someone tell me how Nushell wants to be informed about the dir in which to open?

  3. fdncred commented on Oct 21, 2020

    @fdncred
    Contributor

    I looked into this a bit and it doesn't appear the nushell looks for or knows about an environment variable called PWD on startup. Although, I did notice that when set_path is called it sets the PWD env var. I'm guessing that somewhere in nu-cli/src/cli.rs, perhaps in the fn cli() one would need to check the PWD env var and use it if found, for this functionality to work like you want it to.

  4. zbraniecki commented on Nov 30, 2020

    @zbraniecki
    Author

    @fdncred the intro you provided is enough to get new nushell to use $PWD, yes!

    This makes the following work:

    • Open terminal with bash
    • go to ~/projects
    • Set in Tilix that new terminal is nu
    • ctrl+a to open new terminal
    • Tilix opens new terminal with nu shell in ~/projects
    • in bash cd my-project
    • ctrl+a
    • Tilix opens new terminal with nu shell in ~/projects/my-projects

    That's exactly as expected!

    The final piece of the puzzle I am missing is how does the shell augment the value of $PWD for other (newly created) shells?

    In other words, this doesn't work:

    • Set in Tilix that new terminal is nu
    • Open terminal with nushell
    • go to ~/projects
    • ctrl+a to open new terminal
    • Tilix opens new terminal with nu shell in ~/projects

    It seems that in Gnome there's some game around something called VTE:

    I don't know how bash/zsh inform VTE to save the current directory so that it gets extracted as $PWD in a new shell.

    Anyone has an idea how to approach it?

  5. fdncred commented on Nov 30, 2020

    @fdncred
    Contributor

    My guess would be to call set_path every time one uses the cd command so that the PWD env var gets set. I'm not sure that would work but it may be worth a try.

  6. zbraniecki commented on Nov 30, 2020

    @zbraniecki
    Author

    My guess would be to call set_path every time one uses the cd command so that the PWD env var gets set. I'm not sure that would work but it may be worth a try.

    I tried to plug that into filesystem_shell.rs::cd and it didn't work. And I don't think it should. Environmental variables are per-shell-session, so setting PWD when you change directory changes PWD in that particular terminal session.

    Opening a new one, somehow either Tilix would have to "pick up" the PWD of the previous session and pass it to the new one (I tested if Tilix passes any env::args - it doesn't), or there has to be some system that sets the default PWD value for a new terminal.

    It seems to me that somehow bash/zsh do that and that's why with my patch newly opened nushell has the right PWD, but at the same time nushell just setting PWD does not trigger this cross-session passing of PWD.

    My intuition tells me that since we don't want any new terminal to use the path, just the terminal opened from the given terminal, that it should be Tilix taking the PWD of the terminal in focus when ctrl+a is pressed, and passing it to the new terminal as PWD.

    But, knowing POSIX, I expect my logic to not hold. :)

    @gnunn1 - can you shed some light on how to do this properly? I suspect some black-VTE magic to be involved :(

  7. sousajo-cc commented on Dec 31, 2020

    @sousajo-cc
    Contributor

    Maybe get the cwd of the parent process and pass it to the new prompt?

  8. ilius commented on Feb 11, 2021

    @ilius
    Contributor

    LXTerminal and xfce4-terminal also work, from bash to nu, and sh to nu!

    I think the terminal should set the current directly when running / forking the shell.
    And tilix does not do that?

    I'm not familiar with D language.
    But Python has such feature when running a process: subprocess.Popen has cwd argument.
    Also here there is a workDir argument:
    https://dlang.org/library/std/process/spawn_process.html

    Should be possible to fix in tilix.

  9. vadixidav commented on Apr 9, 2021

    @vadixidav

    Also related to this is likely that when Open in Terminal is used on a directory in Gnome, if you set nushell as the shell, it will open only in the home directory (presumably because it uses PWD to indicate where to open at). Should this have a separate issue, or perhaps should this issue be renamed?

  10. zbraniecki commented on Jun 18, 2021

    @zbraniecki
    Author

    I am setting a new machine and was able to reproduce this in Tilix+bash!

    Tilix showed a warning pointing at https://gnunn1.github.io/tilix-web/manual/vteconfig/

    When I switched to "Run command as login shell" the bug is fixed in bash, but not in nushell.

  11. github-actions commented on Jul 19, 2021

    @github-actions

    This issue is being marked stale because it has been open for 30 days without activity. If you feel that this is in error, please comment below and we will keep it marked as active.

  12. vadixidav commented on Jul 19, 2021

    @vadixidav

    This is still an issue. Including with Open in Terminal with Gnome.

  13. 1 remaining item

  14. github-actions commented on Oct 29, 2021

    @github-actions

    This issue has been marked stale for more than 10 days without activity. Closing this issue, but if you find that the issue is still valid, please reopen.

  15. lucatrv commented on Mar 5, 2022

    @lucatrv
    Contributor

    Depending on the system you're using (on Linux, most distributions apart from Fedora and its derivatives), this issue can potentially affect all terminals based on GNOME VTE. Therefore not only Tilix but also GNOME Terminal and many others.

    The problem is well described in the Tilix documentation, together with two possible solutions. However, solution 1 can only be used with Bash or Zsh, while solution 2 (using a login shell) has significant drawbacks. For instance on most distributions (apart from Fedora and its derivatives) a Bash login shell does not source ~/.bashrc.

    Fish developers have dealt with the same issue in the past, and it was fixed with this PR. Now Fish notifies VTE at every working directory change. Current code is here.

    To fix this issue in Linux, Nushell should behave like Fish.

  16. fdncred commented on Mar 5, 2022

    @fdncred
    Contributor

    WezTerm has a good writeup on this too https://wezfurlong.org/wezterm/shell-integration.html#osc-7-escape-sequence-to-set-the-working-directory.

    Theoretically one could construct an osc 7 alias/def and put it in the prompt block that would maybe fix this with people having this issue. Something along the lines of $"(ansi osc)7;file://(localhost)/($nu.cwd)(ansi esc)\"

  17. lucatrv commented on Mar 5, 2022

    @lucatrv
    Contributor

    I tried issuing command printf "\033]7;file://HOSTNAME/CURRENT/DIR\033\\" manually (of course with my actual hostname and working directory), and it works! All following new shells within the same terminal (for instance in new tabs) open in the last issued working directory.

    I wonder however why Fish checks the terminal type, and issues this command only on supported terminals. Is it only for performance reasons, or could this command give problems on other systems? I can check it only on Linux and it works flawlessly, maybe you should verify it also on Windows and macOS. I suggest to reopen this issue and fix it.

  18. fdncred commented on Mar 5, 2022

    @fdncred
    Contributor

    The terminals have to support osc 7 in order for it to work. I'm not sure how common that is so it may be good to leave it in the hands of the users via a prompt block.

  19. lucatrv commented on Mar 5, 2022

    @lucatrv
    Contributor

    Many Linux terminal emulators are based on VTE. IMHO it would be unfair to treat Linux users differently than the rest of the audience, requiring a hack to make this work. Actually I would expect most of Nushell users to be on Linux, but maybe I'm wrong.
    What happens if the OSC 7 escape sequence is printed on a terminal that does not support it? If this is a potential problem, Nushell could check for supported terminals like Fish does.

  20. endigma commented on Aug 23, 2022

    @endigma

    This is still an issue, is there a fix planned?

  21. fdncred commented on Aug 23, 2022

    @fdncred
    Contributor

    Have you tried the -e argument to nu?

  22. endigma commented on Aug 23, 2022

    @endigma

    as my login shell?

  23. sgasse commented on Jan 26, 2023

    @sgasse

    Hm not sure if this is the same "issue" but just as a hint: I sometimes use the "Open in terminal" in the context menu of the file explorer nautilus. When I switched to nu, I set the default command to /home/myuser/.cargo/bin/nu in the terminal profile of the GNOME terminal. However that opened the shell always in the home directory (as reported for Tilix here) and not in the directory that I wanted it to open in.

    The solution was a setting in the GNOME terminal, nothing in nu. Here is a screenshot (sry for the German, it's my private machine). The important setting was "Keep working directory: Always".

    Maybe this points someone in the right direction. At least searching for my issue led me to this bug here.
    PWD_GNOME_terminal_nu

  24. mb21 commented on Nov 28, 2023

    @mb21

    Apple terminal preferences include this helpful description:

    image

    The working directory may be set using the Operating System Command (OSC) escape sequence:

      ESC ] Ps ; Pt BEL
    

    The parameter Ps is 7 (working directory) and Pt is a “file:” URL. The URL should include a hostname to disambiguate local and remote paths, and characters must be percent-encoded as appropriate.

    So what do I need to put into my nushell config? Can I register some callback/closure function whenever it changes directories?

    Adapting @fdncred's comment from March 2022, I do:

    $env.PROMPT_COMMAND = $"(ansi osc)7;file://(sys | get host.hostname)/($env.PWD)(ansi esc)"
    

    But then new tabs just have the prompt 38;5;14m so I must be doing something wrong :S

  25. fdncred commented on Nov 28, 2023

    @fdncred
    Contributor

    This is not right

    $env.PROMPT_COMMAND = $"(ansi osc)7;file://(sys | get host.hostname)/($env.PWD)(ansi esc)"
    

    Here's another translation of what I originally said (note the trailing \) - This is called ST or string terminator. All of this is called OSC 7.

    $"(ansi osc)7;file://((sys).host.hostname)/($env.PWD)(ansi esc)\\"
    

    However, OSC7 should just work if you have shell_integration set to true in your config.nu file. Are you saying you have that set and it still doesn't work?

  26. mb21 commented on Nov 28, 2023

    @mb21

    Oh, this indeed does the trick on its own:

    $env.config.shell_integration = true
    

    Thanks a lot for the quick reply!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issue

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions