Skip to content

How to make file managers open new windows in home

RedBearAK edited this page Aug 8, 2024 · 3 revisions

If it doesn't make sense to you to have a new file manager window redundantly open the same location as an existing window, and you want your file manager to open either your home or some other custom location every time you open a new window, you will probably need to set up some custom global shortcuts in your desktop environment. Then, if desired, the Toshy config file can have a custom remap for Cmd+N in each app, remapping onto the shortcut that activates your custom command. That way all new windows will open the location you want.

The file managers for GNOME (Nautilus/Files) and KDE Plasma (Dolphin) do not currently have an option in their settings like the Finder in macOS, to tell them to open all new windows in your home folder. This page will explain how to get around this while using custom keymaps in Toshy's config file.

Dolphin, at least on Plasma 6 (possibly on Plasma 5 also) will actually open new windows in your home folder if (and ONLY if) you use the "global" shortcut of Meta+E. Somehow this is set up to cause new Dolphin windows to open in your home folder regardless of whether there are any other Dolphin windows that may have other folders open at the time. And this also ignores the "startup" setting in Dolphin, if you have Dolphin configured to start initially in a certain path/folder. So if you want new Dolphin windows to open a location other than home, this isn't that useful. I'll show how to use custom commands to make either Nautilus or Dolphin open any location you want when you open any new file manager window.

The techniques here can probably be applied to any Linux file manager that accepts a path on the command line, and has a "new window" type of option to force a new window to be opened, rather than focusing an existing window that happens to have that path open. I may update this later with more such examples. The examples will be for using the default file manager in each desktop environment, but of course you can most likely just make the same custom shortcut in another desktop environment if you're using a non-default file manager, as long as the DE makes it possible to create a custom shortcut that runs a command.

Nautilus custom shortcut in GNOME

In the Keyboard settings panel, open up the shortcuts, and find the button that lets you make a custom shortcut. Use this command:

/usr/bin/nautilus --new-window ~

The tilde (~) will expand into your home folder path. Or you can specify the explicit path, like /home/myusername. If instead you want to always open your Documents folder, just change the path:

/usr/bin/nautilus --new-window ~/Documents

Set the shortcut to the physical equivalent Option+Cmd+Space for your keyboard type. This is the shortcut in macOS that will open a new Finder window in Search mode.

Now you can create a keymap in your Toshy config to also remap the physical equivalent of Cmd+N while using Nautilus to invoke this new custom global shortcut. This will force Nautilus to open all new windows in your chosen custom location (wherever that is).

Open your config file, which is at ~/.config/toshy/toshy_config.py. The tray icon provides a quick way to open the Toshy config folder if you need to, with the "Open Config Folder" item.

In the config file, search for "user_apps". This will take you to a marked "slice" where your edits should survive a Toshy upgrade or re-install. You may already have a custom keymap there with some remaps for hardware or media keys, if you're on a laptop. I have inverted function keys, so I always have to set some up to get my volume and brightness keys back.

Instead of adding your remap to the sample "User hardware keys" keymap, make a new one, like this:

###################################################################################################
###  SLICE_MARK_START: user_apps  ###  EDITS OUTSIDE THESE MARKS WILL BE LOST ON UPGRADE

keymap("User hardware keys", {
    # PUT UNIVERSAL REMAPS FOR HARDWARE KEYS HERE
    # KEYMAP WILL BE ACTIVE IN ALL DESKTOP ENVIRONMENTS/DISTROS

}, when = lambda ctx:
    cnfg.screen_has_focus and
    matchProps(not_clas=remoteStr)(ctx)
)

keymap("User overrides: Nautilus", {
    C("RC-N"):            C("Alt-RC-Space"),                   # Open new window with custom global shortcut
}, when = matchProps(clas="^org.gnome.nautilus$|^nautilus$") )

###  SLICE_MARK_END: user_apps  ###  EDITS OUTSIDE THESE MARKS WILL BE LOST ON UPGRADE
###################################################################################################

Save the changes to the config file, and restart Toshy from the tray icon or with the command toshy-services-restart in the terminal. (Leave the keyboard alone for several seconds while the keymapper process starts up.) You should find that using the physical equivalent of Cmd+N in Nautilus/Files will open a new window in your chosen location, as long as the global shortcut also works.

Dolphin custom shortcut in KDE Plasma

Open the Shortcuts settings panel in System Settings (you can go right to it from the app launcher menu or krunner). Click the "Add New" button, and choose "Command or Script...", then enter the command to run:

/usr/bin/dolphin --new-window ~

Give the new shortcut a combo by pressing the physical equivalent of Option+Cmd+Space. On a PC keyboard that will be Meta+Alt+Space. KDE likes to refer to the Meta/Super/Win key as "Meta", while GNOME calls it "Super". On an Apple keyboard you will of course just press the actual Option+Cmd+Space keys. The resulting shortcut that needs to be used in the Toshy config file in either case will be "Alt-RC-Space", just like in the Nautilus example above.

Same as above, make a new keymap in the "user_apps" marked slice. You can have the keymap for Nautilus and the keymap for Dolphin at the same time. The conditionals will make them active only in the corresponding application windows. I'll just go ahead and include the Nautilus keymap here in the example to illustrate.

###################################################################################################
###  SLICE_MARK_START: user_apps  ###  EDITS OUTSIDE THESE MARKS WILL BE LOST ON UPGRADE

keymap("User hardware keys", {
    # PUT UNIVERSAL REMAPS FOR HARDWARE KEYS HERE
    # KEYMAP WILL BE ACTIVE IN ALL DESKTOP ENVIRONMENTS/DISTROS

}, when = lambda ctx:
    cnfg.screen_has_focus and
    matchProps(not_clas=remoteStr)(ctx)
)

keymap("User overrides: Nautilus", {
    C("RC-N"):            C("Alt-RC-Space"),                   # Open new window with custom global shortcut
}, when = matchProps(clas="^org.gnome.nautilus$|^nautilus$") )

keymap("User overrides: Dolphin", {
    C("RC-N"):            C("Alt-RC-Space"),                   # Open new window with custom global shortcut
}, when = matchProps(clas="^dolphin$|^org.kde.dolphin$") )

###  SLICE_MARK_END: user_apps  ###  EDITS OUTSIDE THESE MARKS WILL BE LOST ON UPGRADE
###################################################################################################

Save the config, restart Toshy from the tray icon menu or with toshy-services-restart in the terminal. You should now find that Dolphin will open a new window in your chosen path not just when you use your custom global shortcut combo of Option+Cmd+Space, but also when you are in Dolphin and use Cmd+N, which will remap onto that custom global shortcut. (On a PC keyboard we are referring to the physical equivalents of those key locations on an Apple keyboard.)

Opening multiple paths in each new window

Dolphin seems to open multiple paths given to it on the command line by opening them all in one window, as tabs. It does this even if you haven't enabled the option in Dolphin to open all new folders in tabs. So it's feasible to have all new Dolphin windows created via the custom shortcut open more than one location, in tabs. The last path passed to the command will be the tab that has focus. So doing it like this will cause the home folder to be in the focused tab:

/usr/bin/dolphin --new-window ~/Documents ~/Downloads ~

Nautilus, when I tried this in the terminal, opened all the folders in different windows instead of tabs. There's no option I can find to change this behavior to use tabs instead of separate windows, so it's less practical to send multiple paths to Nautilus, unless you're doing it from a startup script just to have some windows open at different locations when you log in. I wouldn't do it from the custom global shortcut command.

Using multiple file managers in the same DE (or multiple DEs)

You may notice that you can only set the Option+Cmd+Space custom global shortcut to invoke one file manager. Normally this won't be a problem, since most users will use only one file manager, and usually the default for each DE.

If for some reason you wanted to use both Nautilus and Dolphin in KDE Plasma, for instance, you'd have to give the custom shortcut for one of the file managers a different key combo, and this would need to be reflected in the keymap for that file manager. The differing combo would also need to be the same in other desktop environments if you had multiple DEs, like GNOME and Plasma, installed on the same system. Otherwise the remap in one of the keymaps would not work correctly in the other DE.

Well, technically you could set up separate sets of keymaps that only come alive in a certain desktop environment, but that would be kind of mental. Let's do it anyway! Sounds like fun.

###################################################################################################
###  SLICE_MARK_START: user_apps  ###  EDITS OUTSIDE THESE MARKS WILL BE LOST ON UPGRADE

keymap("User hardware keys", {
    # PUT UNIVERSAL REMAPS FOR HARDWARE KEYS HERE
    # KEYMAP WILL BE ACTIVE IN ALL DESKTOP ENVIRONMENTS/DISTROS

}, when = lambda ctx:
    cnfg.screen_has_focus and
    matchProps(not_clas=remoteStr)(ctx)
)

if DESKTOP_ENV == 'gnome':
    keymap("User overrides: Nautilus", {
        C("RC-N"):            C("Alt-RC-Space"),                   # Open new window with custom global shortcut
    }, when = matchProps(clas="^org.gnome.nautilus$|^nautilus$") )

    keymap("User overrides: Dolphin", {
        C("RC-N"):            C("Shift-Alt-RC-Space"),             # Open new window with custom global shortcut
    }, when = matchProps(clas="^dolphin$|^org.kde.dolphin$") )

if DESKTOP_ENV == 'kde':
    keymap("User overrides: Nautilus", {
        C("RC-N"):            C("Shift-Alt-RC-Space"),             # Open new window with custom global shortcut
    }, when = matchProps(clas="^org.gnome.nautilus$|^nautilus$") )

    keymap("User overrides: Dolphin", {
        C("RC-N"):            C("Alt-RC-Space"),                   # Open new window with custom global shortcut
    }, when = matchProps(clas="^dolphin$|^org.kde.dolphin$") )

###  SLICE_MARK_END: user_apps  ###  EDITS OUTSIDE THESE MARKS WILL BE LOST ON UPGRADE
###################################################################################################

Ludicrous? Maybe, maybe not. Here we have a set of keymaps that will do different things depending on which desktop environment you log into. You can have a custom global shortcut of Option+Cmd+Space for the default file manager in each DE's own settings, and then another custom shortcut of Shift+Option+Cmd+Space for the "other" file manager. And the customized Cmd+N shortcut in each file manager would "just work" no matter which DE you logged into.

Note that I didn't bother to give all four keymaps unique names, which is usually a really good idea to avoid troubleshooting issues with remaps not working (keymaps with identical names can "shadow" each other). In this case only one pair of the keymaps would ever be established at a time, so they would never conflict with each other. But normally you'd want to have a unique name for each keymap in the config file. Sometimes the unique names can also be helpfully informative, in case conditionals are not quite right:

if DESKTOP_ENV == 'gnome':
    keymap("User overrides: Nautilus in GNOME", {
        C("RC-N"):            C("Alt-RC-Space"),                   # Open new window with custom global shortcut
    }, when = matchProps(clas="^org.gnome.nautilus$|^nautilus$") )

    keymap("User overrides: Dolphin in GNOME", {
        C("RC-N"):            C("Shift-Alt-RC-Space"),             # Open new window with custom global shortcut
    }, when = matchProps(clas="^dolphin$|^org.kde.dolphin$") )

if DESKTOP_ENV == 'kde':
    keymap("User overrides: Nautilus in KDE", {
        C("RC-N"):            C("Shift-Alt-RC-Space"),             # Open new window with custom global shortcut
    }, when = matchProps(clas="^org.gnome.nautilus$|^nautilus$") )

    keymap("User overrides: Dolphin in KDE", {
        C("RC-N"):            C("Alt-RC-Space"),                   # Open new window with custom global shortcut
    }, when = matchProps(clas="^dolphin$|^org.kde.dolphin$") )