Skip to content
sandwich edited this page Oct 23, 2023 · 37 revisions

Here is a place for useful custom key bindings. Share what you got!

If you're not sure where to start, you can find the default keybindings in rc.conf and use those for inspiration.

Quick editing rc.conf

map X chain shell vim -p ~/.config/ranger/rc.conf %rangerdir/config/rc.conf; source ~/.config/ranger/rc.conf

Explanation: By starting the binding with a "chain" command, we can supply multiple commands separated by ";". The first command opens vim with the custom rc.conf and the default rc.conf as a reference in two separate tabs. When you close vim, the second command will source the custom config, so that your changes take effect in the current ranger instance.

Smart "cw"

map cw eval fm.execute_console("bulkrename") if fm.thisdir.marked_items else fm.open_console("rename ")

When typing cw, usually the console is opened with the content "rename " for quick renaming. But ranger includes a feature for quickly renaming multiple files at once, the so-called "bulkrename" command. The mapping above will check whether you have any selected files in the current directory and pick the appropriate renaming method.

Send selected file(s) to trash

map DD shell mv -i %s ~/.local/share/Trash/files/ && echo "trashed %s        from_dir $(pwd)" >> ~/.config/ranger/trash_history 
map uD shell fileToRestore=$(cat ~/.config/ranger/trash_history | grep "^trashed" | tail -n 1 | cut -c10- | awk -F"from_dir " '{print $1}' | rev | cut -c10- | rev) && mv -i ~/.local/share/Trash/files/"$fileToRestore" "$fileToRestore"1. 1.  && echo "         untrashed $(pwd)/"$fileToRestore"" >> ~/.config/ranger/trash_history && unset fileToRestore
map Dh shell less ~/.config/ranger/trash_history

Send selected file(s) to trash for macOS

map DD shell mv %s ~/.Trash && echo "trashed %s from_dir $(pwd)" >> ~/.config/ranger/trash_history
map uD shell fileToRestore=$(cat ~/.config/ranger/trash_history | grep "^trashed" | tail -n 1 | cut -c10- | awk -F "from_dir " '{print $1}' | rev | cut -c3- | rev) && mv -i ~/.Trash/$fileToRestore $fileToRestore && echo "untrashed $(pwd)/$fileToRestore" >> ~/.config/ranger/trash_history && unset fileToRestore
map Dh shell less ~/.config/ranger/trash_history

Use DD to send one or several files to the usual trash folder. Restore the last file sent to the trash using uD. File restoration puts the new file in the current folder, not where you trashed it. Access Trash history with Dh. You can trash several files at once but you can't restore them using uD, to do so you would have to use Dh to help you restore them manually.

Filter-as-you-type "f"

map f console scout -ftsea%space

Mimics the behaviour of find, but filtering files as you type.

Recursive search

map F chain flat 5 ; console scout -ftsea%space

Allows to search recursively by first unfolding each folder and subfolder then filtering as you type (similar to the command above). Keepking flat leval to 5 can help avoiding freezing with large folder, in which case you're probably better of using locate for example. For infinite level searching use flat -1. Add the -l arguments to the scout command to use approximate matching.

Always fork when using "open_with"

map r chain draw_possible_programs; console -p10 open_with f

This adds the flag f to the command invoked with r, this is useful if you mostly invoke commands that have a window of their own where you can issue commands with the r menu, think graphical applications like a pdf reader.

Go to currently played song in MPD

map go eval from ranger.ext.spawn import spawn; fm.select_file("/media/external/music/" + spawn("mpc -f %file% | head -1"))

This command moves to the song that is currently played in MPD, the music player daemon. You have to specify the root directory of MPD in the command. Here is a version that extracts the root directory from /etc/mpd.conf, but it may be unreliable:

map go eval from ranger.ext.spawn import spawn; fm.select_file(spawn("grep ^music_directory /etc/mpd.conf | grep -oP '(?<=\").*(?=\")'").strip() + "/" + spawn("mpc -f %file% | head -1"))

Integrate with tmux splits and windows

Open highlighted files in splits, windows

  • map ef eval exec('try: from shlex import quote\nexcept ImportError: from pipes import quote\nfm.run("tmux splitw -h rifle " + quote(fm.thisfile.basename))')
  • map ev eval exec('try: from shlex import quote\nexcept ImportError: from pipes import quote\nfm.run("tmux splitw -v rifle " + quote(fm.thisfile.basename))')
  • map ew eval exec('try: from shlex import quote\nexcept ImportError: from pipes import quote\nfm.run("tmux new-window rifle " + quote(fm.thisfile.basename))')

The above three commands open the highlighted file with whatever you've told rifle to use to open it - useful if you want to open a file while still using your current ranger instance.

An alternative way,

# ranger will handle quoting for macro %f automatically
# analogous to vim's :Vexplore, :Sexplore/Hexplore
map ev shell tmux split-window -h rifle %f
map es shell tmux split-window -v rifle %f
map ew shell tmux new-window rifle %f

Create splits, windows from current directory

Use keybindings defined in .tmux.conf to create windows or panes from current directory.

# inherit current path in new window, pane/split
bind c new-window -c "#{pane_current_path}"
bind '"' split-window -v -c "#{pane_current_path}"
bind % split-window -h -c "#{pane_current_path}"

Or use the shell command in ranger.

# analogous to vim's :Vexplore, :Sexplore/Hexplore
map ev shell tmux split-window -h -c %d
map es shell tmux split-window -v -c %d
map ew shell tmux new-window -c %d

Toggle visibility of files

This dotify.sh script will rename files, toggling their visibility (e.g. file -> .file and vice-versa):

#!/usr/bin/sh

for path in "$@"; do
    file_name="$(basename "$path")"
    dir_name="$(dirname "$path")"
    case "$file_name" in .*)
        mv -v -- "$path" "$dir_name/${file_name#.}"
        ;;
    *)
        mv -v -- "$path" "$dir_name/.$file_name"
        ;;
    esac
done

printf "Press ENTER to continue"
read -r

After saving and chmodding (chmod +x dotify.sh) the script, you can define a key binding in ranger to execute it on selected files:

map <alt>h shell /path/to/dotify.sh %s

Unmount and power-off devices

map xp shell for selection in %p; do path=$(findmnt -o SOURCE -n $selection); udisksctl unmount -b "$path" && udisksctl power-off -b "$path"; done

Use this shortcut on a mount point (the folder where you mounted your device) or a selection of mount points (as it send the selection to a loop) to unmount and power-off the underlying device(s).