2

I use fish shell and Gnome-Terminal Version 3.44.

When I open a new terminal I'd like it to start up in the previously-visited directory. How can I implement this via fish shell?

4
  • 1
    Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer.
    – Community Bot
    Commented May 19, 2022 at 21:23
  • How would you do this in any shell? That's not rhetorical. You'll need to modify your shell environment to save the CWD and read the saved value when starting a new shell. Commented May 19, 2022 at 22:17
  • Do you want the chronologically most recent directory? How do you define that if you run multiple shells in parallel? Or do you want to open the new (second or subsequent) terminal in the directory where your current terminal's fish resides? If the latter, you need to emit the OSC 7 escape sequence whenever you change directory.
    – egmont
    Commented May 20, 2022 at 6:38
  • Yeah, sorry if this sounded vague. By default my previous ubuntu / gnome terminal automatically opened the last directory i was located in, but this functionality went away when i updated to latest ubuntu so was trying to get back to that default functionality. Commented May 23, 2022 at 19:55

1 Answer 1

3

You have a few options as to when to save the directory:

  • When you exit the shell
  • Or each time the directory changes

I'll use the "each time" approach for this example. First, I recommend exiting all but one terminal/fish shell. This will allow subsequent shell invocations to load the new function properly.

Create a Fish script that runs at startup:

~/.config/fish/conf.d/starting_dir.fish:

set -q fish_most_recent_dir && [ -d "$fish_most_recent_dir" ] && cd "$fish_most_recent_dir"

function save_dir --on-variable PWD
    set -U fish_most_recent_dir $PWD
end

This will:

  • check that the fish_most_recent_dir variable exists, that it refers to an existing directory, and changes to it if so.

  • create a save_dir function that saves the current directory to the fish_most_recent_dir universal variable whenever the directory changes (--on-variable PWD).

The save_dir event function needs to be forcibly loaded at startup this way because autoload functions can't handle events in fish.

Note that if there are any other cd statements in your startup config, they may get executed after this function. If that happens, they'll override this.

If you want to save the working directory only when Fish exits, you would change the event function to use --on-event fish_exit instead.

7
  • not sure why but the save_dir func doesn't seem to actually fire when changing directory. I'm testing on WSL Ubuntu 18.04. Commented May 23, 2022 at 19:52
  • just to clarify. this function with --on-variable does not seem to fire ever beyond the very first time. Commented May 23, 2022 at 20:03
  • @quiltedcomputer Hmm - Also on WSL, but was using the latest release (3.4.1) on (probably Artix). So I just tested on on Ubuntu 18.04 on WSL with Fish 2.7.1 (the default there), but it's still working for me there as well. After each directory change, set --show fish_most_recent_dir will show the updated directory. You mention running gnome-terminal -- Are you running that on WSLg (Windows 11) or using some other method? I just added add gnome-terminal, and it looks like the latest version on Ubuntu 18.04 was 3.28.2. But the function still fires on every cd there as well ... Commented May 23, 2022 at 20:07
  • @quiltedcomputer Ahh, I left out a critical step that could be what you are seeing (although it doesn't quite sound like it according to your description). But try funcsave save_dir after creating the function and see if that helps. Commented May 23, 2022 at 20:10
  • Yes, I understand the function needs to be permanently saved. But this solution unfortunately never worked for me. I'm now also testing on Ubuntu 22.04 with fish 3.4.1. In particular, set --show fish_most_recent_dir produces no output (just an empty newline). When opening a new terminal, it also opens the home directory. Commented Aug 14, 2022 at 15:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .