How can I make Nautilus file manager remember my open tabs?

Is it possible to preserve my open tabs between closing and opening of Nautilus file manager?

Preferably even across different logouts and restarts.


Solution 1:

(Very) limited command line options of nautilus

Unfortunately, nautilus does not offer command line options to read the opened directory of its windows, nor does it have any option to send an existing window to another directory. Since you cannot remember what you do not see, we are running out of options at first sight.

However

We do have xdotool, not to do what nautilus doesn't, but to at least fake the behaviour that you describe. We can do that in such a way that "you would believe it" if you didn't know how it is done.

Although the solution below does not survive a restart, "remembering" (possibly tabbed) window and all opened directories is very well possible within one session. Since you mentioned to be interested in that as a "second choice", here it is.

How it works; the process

Although We cannot close a window and preserve its tabs and opened directories, we can make an existing window seemingly (and completely) disappear, with the help of xdotool.

If we subsequently change the behaviour of the nautilus launcher in such a way that it first looks for possible unmapped windows to remap, before opening a new one, effectively we have exactly the same behaviour as if nautilus would remember the last used window(s).

How to set up

  1. Copy the script below into an empty file, save it as remember.py

    #!/usr/bin/env python3
    import subprocess
    import os
    
    app = "nautilus"
    
    wfile = os.environ["HOME"]+"/.unmapped_"+app
    
    def get(cmd):
        # simply a helper function
        return subprocess.check_output(cmd).decode("utf-8").strip()
    
    def check_windowtype(w_id):
        # check the type of window; only unmap "NORMAL" windows
        return "_NET_WM_WINDOW_TYPE_NORMAL" in get(["xprop", "-id", w_id])
    
    def get_pid(app):
        # (try to) get the pid of the application 
        try:
            return get(["pgrep", app])
        except subprocess.CalledProcessError:
            pass
    
    def get_matches(pid):
        # get the window list, select the valid (real) app's windows
        ws = get(["wmctrl", "-lpG"]).splitlines()
        matches = [w.split() for w in ws if pid in w]
        return [w for w in matches if check_windowtype(w[0]) == True]
    
    try:
        # try to read the file with unmapped windows
        wininf = [l.split() for l in open(wfile).readlines()]
    except FileNotFoundError:
        # if there are no, unmap the current app's windows
        filebrowserwins = get_matches(get_pid(app))
        if filebrowserwins:
            open(wfile, "wt").write(("\n").join((" ").join(l) for l in filebrowserwins))
            for w in [w[0] for w in filebrowserwins]:
                subprocess.Popen(["xdotool", "windowunmap", w])
        else:
            arg = "--new-window" if app == "nautilus" else "" 
            subprocess.Popen([app, arg])
    else:
        # re- map unmapped windows
        for w in wininf:
            wid = w[0]; geo = w[3:7]
            subprocess.call(["xdotool", "windowmap", wid])
            subprocess.Popen(["wmctrl", "-ir", wid, "-e", "0,"+(",").join(geo)])
        os.remove(wfile)
    
  2. The script needs both wmctrl and xdotool:

    sudo apt-get install wmctrl xdotool
    
  3. Copy the nautilus launcher from /usr/share/applications to ~/.local/share/applications

    for 15.04 and later:

    cp /usr/share/applications/org.gnome.Nautilus.desktop ~/.local/share/applications
    

    for earlier Ubuntu versions:

    cp /usr/share/applications/nautilus.desktop  ~/.local/share/applications
    
  4. open the local copy with gedit:

    gedit ~/.local/share/applications/org.gnome.Nautilus.desktop
    

    (in case of 15.04 +)

    and look for the first line, starting with Exec=. Change it into:

    Exec=python3 /path/to/remember.py
    

    Save and close the file.

  5. Create a keyboard shortcut with the same command: Choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

    python3 /path/to/remember.py
    
  6. Now log out and back in

How to use

The use is very simple:

  • To open a window, do as usual: click on the nautilus launcher. Tab the window as you like:

    enter image description here

  • To close a window definitively, close it by clicking on the window's "close" (x) box.

  • To preserve the window(s) + all its tabs:
    press the shortcut key. The window(s) will vanish (seemingly close).

    enter image description here

  • Next time when you click the launcher, the nautilus window(s) will appear exactly like the last time, even the window position(s) will be preserved.

    enter image description here

That's it

Note

  • In the tests I ran, I couldn't find any downside (14.04). If you might run into any, please mention!
  • In the How to use -section, I suggested to click on the nautilus icon to remap possible unmapped windows. The shortcut key will do the same however, so you can see what works the most convenient for you. Also, if you opened a folder by double click after you unmapped on or more folders, unmapped folders still will be remapped by the shortcut.

EDIT:

Nemo users

Nemo users can equally use the solution above, but:

  • In the head section of the script, change:

    app = "nautilus"
    

    into:

    app = "nemo"
    
  • In point 3, use:

    cp /usr/share/applications/nemo.desktop ~/.local/share/applications
    
  • In point 4, use:

    gedit ~/.local/share/applications/nemo.desktop
    

Tested, proved to be working with nemo

Solution 2:

I recommend using other file manager instead if that's okay with you since Nautilus doesn't have that feature.

Here's one alternative app that does the trick: SpaceFM

Here's how to install it.

It has rich features such as, of course, reopen last tabs.

To make it the default file manager:

xdg-mime default spacefm.desktop inode/directory

Solution 3:

Try out these scripts to save and restore tabs of nautilus file manager. https://github.com/susurri/nautilus_save_tabs/