I'm using fedora 25 gnome edition, zsh shell. I can right click, open folder in terminal. I configured gnome terminal to start tmux when it gets opened.

Now if I open folder in terminal, the terminal would open the default user directory instead of the actual directory I'm in. How to fix it?

enter image description here

I need to either pass additional parameters to tmux in gnome-terminal, or to Nautilus.

If that works with another file manager please let me know, and I'll consider the possibility of switching, but if it can be done using Nautilus, it's better.

  • Not to detract form the good answer below, but for future reference Dolphin has that functionality built-in with three options. One is to open a terminal in the the current directory. Second is to right-click and open a terminal in the selected directory directory. Third, my choice, is to open a panel in Dolphin in the current directory. With the last option, directory changes in the file panel and in the terminal panel stay in sync. And, the directory changes made in the file panel don't add cd commands to the command history of the terminal. Don't know if it can use zsh shell over bash. – Chindraba Feb 26 '17 at 12:20
  • @GypsySpellweaver the answer below present two ways, the second method isn't working and I don't like nested sessions. I might give dolphin a go if nothing else worked, thanks – Lynob Feb 26 '17 at 12:39
  • It is a bug that has been reported. Relevant bug reports: cwd should be respected for custom commands, terminal-nautilus doesn't set the correct directory for custom commands – YurB Jan 10 '18 at 9:31

I haven't used Linux as my main driver in a while, but there used to be a program nautilus-open-terminal for doing this, not sure if it's supported on Fedora though. Also, though I don't know if it's possible to install it on non-Ubuntu/Debian systems Canonical's Unity DE has this functionality built in, you could probably replicate the method used there in Gnome.

EDIT: In light of your comment, you might add the lines to your .profile to always open tmux in the current working directory of gnome-terminal as a disowned process (i.e. tmux ./; disown -r && killall gnome-terminal) so that when you right click and open terminal in directory it opens a gnome-terminal, and immediately opens an independant tmux windows, passing the working directory recieved by nautilus as an argument.

UPDATE: Okay, I have found two methods. Now each of these have their own issues, but hopefully one of them will work well enough for you. These methods were tested on a Debian 8.7 installation using Bash as the shell and the program nautilus-open-terminal to launch a terminal window in the current Nautilus/Nemo directory.

Method 1.) Simply add tmux to the very end of your .bashrc file (or in your case .zshrc.). Now any terminal window you open will immediately open tmux as a process in the working directory.

The pro to this method is it is simple and only opens one window.

The major con to this method is it will always run as a nested process in the current shell, so if that's a problem you may want to use the other method instead.

Method 2.) Add the line gnome-terminal -e tmux ./ (no quotes around the process name this time) to your .bashrc or .zshrc file. This will open a new terminal window that contains tmux as the main process.

The pros for this method are that the tmux window will be a detached process, and will still open with the current working directory set to the Nautilus/Nemo window's directory.

The con is that while it opens a tmux dedicated window, exit cannot be added to the .bashrc file or it will kill the tmux window as well. With that said the gnome-terminal that is used to spawn the tmux window can still be closed manually by clicking the x on the window, or running exit directly in the gnome-terminal once tmux has opened.

While the second method is less convenient in that you have to manually close the excess terminal window; it will open a dedicated tmux window in the current directory when the Open In Terminal context menu option is clicked.

| improve this answer | |
  • AFAIK nautilus-open-terminal is now gnome-terminal-nautilus and it's responsible for showing terminal in right click menu and I already have it. What I don't know how to do is how to pass the current directory to tmux from it – Lynob Feb 20 '17 at 21:57
  • First of all, if you decide to edit your answer please notify me by writing a comment, stackexchange doesn't notify the OP whenever a user edits his answer, I found out that you did so by luck. – Lynob Feb 22 '17 at 22:22
  • if you mean by .profile, the terminal profile. I tried that just now and the terminal stops working, it says The child process exited normally with status 1. – Lynob Feb 22 '17 at 22:23
  • @Lynob try putting it in .zshrc instead, you may also try using gnome-terminal -e "tmux ./" in place of the previously suggested command (once again in the .zshrc file instead of .profile.). – Alison E.E. Feb 23 '17 at 1:33
  • Now, in .zshrc, gnome-terminal -e "tmux ./" gives The child process exited normally with status 1. and tmux ./; disown -r && killall gnome-terminal gives unknown command: ./ /home/user/.zshrc:disown:173: job not found: -r – Lynob Feb 23 '17 at 9:39

I have added:

# Start tmux if tmux is not already running
pgrep tmux >/dev/null 2>&1 || exec tmux

at the end of my .bashrc and it looks to me that everything is working fine. When I start a gnome-terminal manually (for the first time), tmux starts. If I open other terminals, they won't have tmux (and this is pretty much what I want). And opening a terminal from any folder in Nautilus (with "Open in Terminal") also works as expected.

| improve this answer | |

With TMUX environment variable, we can use exit to automatically kill the original terminal when exiting the tmux session.

if [[ "$TMUX" == "" ]]  # if not on tmux

It also prevents "sessions should be nested with care, unset $TMUX to force" message caused by calling tmux within a tmux session.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.