4

Pressing Alt+Home in Nautilus takes my directly to /homer/user. This is very convenient!

I would like to setup a custom keyboard shortcut for an arbitrary folder in my system, since besides /home/user there are a few other folders I need to access frequently when going through various folders on my system (e.g. /media/user/my_pictures, if I have mounted an external hard drive containing a lot of pictures and videos I have taken).

I would like one single shortcut combination to take my there; that means no fiddling solutions, such as bookmarking the folder, then entering with F6 the bookmarks tab and then navigating there to the desired folder. It has to be done in one stroke, just like Alt+Home takes me to my home folder.

l7ll7
  • 385
  • I may be able to come up with something if I find an answer to this question: https://askubuntu.com/q/1387251/558158 – vanadium Jan 15 '22 at 16:01
  • I am afraid that, 10 years later, a hack using keyboard simulation (xdotool) will be the only option: see https://askubuntu.com/a/123801/558158. Nautilus remains ridiculously limited in command line options: you can't even open an new tab from the command line. – vanadium Jan 15 '22 at 17:25
  • @vanadium omg, it's crazy in what a bad state really widely-used software can be. xdotool (resp. xclip, as the answer you linked mentioned it was updated) looks promising. As long as the solution works, it doesn't need to be elegant. Do you think you could post a solution using xdotool? I could then accept it; the answer you linked looks rather technical, I'm not sure I could adapt it to my scenario. – l7ll7 Jan 17 '22 at 15:02
  • Another idea: Should Alt+Home be hardcoded in the source code somewhere? Perhaps one could add one more line there and then compile Nautilus from source? (Although at least for the problem the OP had in the answer you linked, one answer stated that his problem was not solvable that way, so I' not sure about mine either.) – l7ll7 Jan 17 '22 at 15:09
  • That other answer is also a hack using xdotool. Yes, I can post something with xdotool that kinda works. – vanadium Jan 17 '22 at 16:56

2 Answers2

2

In Nautilus, it is unfortunately not possible to assign a shortcut key to open a bookmark, or otherwise display the contents of a specific folder in the current window. The command line interface of nautilus also is extremely limited. It is easy enough to create shortcut keys that opens a folder in a new window, but it is not obvious to change the displayed folder in the current window using a shortcut key.

Potentially, the nautilus-python API exposes the needed functionality, but this requires some programming skills and access to the apparently quite hidden documentation. We hope such answer will come along.

Following is only a hack. Keyboard input to change to another folder using the pathbar is generated using xdotool. This can be started from within nautilus using a nautilus script. There is an obscure feature of nautilus, where one can define a shortcut to execute a script. All together, this would give the effect we are after.

Hack using xdotool

  1. Create a nautilus script:

    gedit ~/.local/share/nautilus/scripts/_1 Documents

  2. Paste the following code for the script:

code

#!/bin/bash
DESTINATION=~/Documents
OLDSELECT=$(mktemp)
echo "$DESTINATION" | xsel -bi
xdotool sleep 0.2 key Control+l Control+v Return
xsel -bi < "$OLDSELECT" ; rm "$OLDSELECT"

/code

  1. Define a shortcut key for the script:

    gedit ~/.config/nautilus/scripts-accels

  2. Add a line looking like

    <Control>0 _1 Documents

This implements a shortcut key Ctrl+0 that will change the current view to the desired folder, ~/Documents in this example. It simulates opening the path bar (Ctrl+L, then pasting the folder path, then hitting Enter.

The script saves textual content that currently may be in the clipboard, stores the desired pathname in the clipboard, then uses xdotool to change the folder using the pathbar. As usual, a small delay (sleep 0.2) is needed to make the script more reliable.

It is a hack. There are several caveats:

Caveat 1: will not work on Wayland

xdotool and xsel do not work on Wayland, the default display server starting from Ubuntu 21.10. Either change to the Xorg session, or adapt the script. Tools like ydotool and wl-clipboard can simulate keyboard input and manipulate the clipboard on Wayland.

Caveat 2: scripts-accels broken in some versions of Nautilus

~/.config/nautilus/scripts-accels, the configuration file that allows to assign shortcut keys to scripts, is broken on Ubuntu 20.04. It worked before, and, fortunately, works again on Files 40.2 (Ubuntu 21.10).

Workaround

One may define desktop wide shortcut keys instead, and in the script, check whether the active window is Nautilus before executing the rest (wmctrl). Drawback: these keys cannot anymore be used in another application/context for something else.

Caveat 3: limited keys can be assigned

The mechanism ~/.config/nautilus/scripts-accels does not allow to override existing keys. For example, keys like <Control>1 and <Control>2 are already used to change the nautilus view. <Alt># keys are already used to change tabs. So experiment which work and which not. Keys are defined like:

; Example Keybinds
; Modifiers: <Control> <Alt> <Shift>
; F4 open-terminal-here
; <Alt>x remove-extension
vanadium
  • 88,010
  • "Following is only a hack." But a really good hack! +1 Thanks a lot for posting such a thorough answer! How are you able to give such a detailed answer for various versions of Ubuntu, do you have a whole desktop farm running somewhere? ;) (Indeed, I have Ubuntu 20.04, so it is useful to know I have to experiment with wmctrl.) – l7ll7 Jan 19 '22 at 07:56
  • Before I accept, could you please let me know, since Alt+Home jumps to the home folder, I would like a similar kind of key combination, for example, Alt+ End, to jump to my desired folder. Do you know how I can input this key combination in the text file? – l7ll7 Jan 19 '22 at 08:00
  • No, I don' t, and I do not know of any documentation except for the lines I provided in the answer – vanadium Jan 19 '22 at 08:26
1

In GNOME, you can set a custom keyboard shortcuts to open Nautilus at specific folder.

Go Settings > Keyboard > View and Customize Shortcuts > Custom Shortcuts and use the command

nautilus /home/user/Downloads/

to make Nautilus open a new window with your Downloads folder open.

You may not always want to open a new window. Perhaps you have a Downloads window open somewhere. To bring that window to the front, you can use the excellent and very versatile Run or Raise extensions.

Bonus: Both Dolphin and PCManFM allows you to define system-level shortcuts that open new tabs at specified locations in the currently open window. With Run or Raise, you can then have just one file manager window open.

Rasmus
  • 8,375