I was looking for a tidy way to navigate directories in a bash shell, so that I could speed up my work a bit. I'm familiar with `cd` command and Tab key completion of names, but sometimes my mind goes blank, so having a visible list of directories to choose from would be way better for me. Actually, listing the current directory contents with `ls` and using the cursor keys and Enter to navigate would have simplified things for me, if that had been possible. But then my search brought me across Roman Zimbelmann's impressive python project, Ranger - the terminal based file browser.
Ranger, in fullscreen, showing directories
the content of a text file shown in the pager
an image file displayed inline
EXIF data displayed for an image file
media info shown for a video file
media info shown for an audio file
content shown for an archive
version control aware for github repo
Install
download ranger (zip file) from Ranger git
unpack, and run `sudo make install'
or clone from git for latest version (need git installed)
If Ranger crashes with a python error about misuse of Tab indentation in multiple files, try downloading a previous release from Github (ranger/releases), just unzip enter the ranger dir and run ./ranger.py, and if it runs then install it with sudo make install
optional installs for Ranger:
w3m and w3m-image are responsible for in-line preview of images (only with rxvt, urxvt and xterm)
To release media drives that could be caught in a process such as w3mimgdisplay, then you will need to use Ctrl-C before ejecting.
After installing, copy the config files from /usr/local/lib so that you can make modifications
Note, the config files may be incompatible with new versions of ranger. Run 'man ranger' for details
Ranger is installed to /usr/local/lib/python2.7/dist-packages/ranger so on new installs you could copy, say, only the scope.sh and rc.conf files over, which are always being upgraded - backup your old ones first and then do a meld compare with the latest versions.
Check out the ranger Wiki to see how to update ranger.
What you can do is copy your original config files, install a newer ranger, run the above command and compare new and old contig files with meld
launches ranger from the current dir in shell (I make a bash alias rr)
S
in Ranger, drops to a shell at current dir (from where Ctrl-D or `exit' returns to Ranger)
q
quits Ranger
Features
displays two columns: active directory level at center, upper dir level at left; and a preview pane at the right
navigate with cursor keys: right/left move across the columns of dir levels (i.e. down/up the directory tree), and down/up moves the highlight down/up the file list in active level column
mouse roller also moves up/down the file list
[ and ] keys move into next or previous dir on same dir level without returning up a level and down into the next dir (which would be same as left, up or down, right)
mouse click on dir or file moves highlight to it
Enter, right-click on dir/file or click to blank space at 3rd column at right, enters the dir or opens the file
Spacebar selects files, which then can be yanked (copied) or cut, or have a process run on them
copied or cut files are stored by path and name on a copy buffer, which can be added to from multiple dir's, or displayed, or manipulated anyway you like
copied files can be added to the queue when pasting so as to not launch multiple file operations all together
'v' selects all if nothing is selected, again and it deselects all, and if some are selected with Space then 'v' inverts selection, 'uv' deselects all if some are selected
if files are selected they remain so when leaving the dir and will still be selected when returning to the dir
able to search for string in file names, search and auto-enter dir or auto-open file, search and filter the files displayed as you type, or search and select files with string
by default all text files, pdf, html and other recognised file types can be previewed in the right-hand pane
mediainfo shows details of any audio/video file while exiftool does for image files
any dir can be bookmarked with a character and easily reached with 'find tag' and 'next' keys
files can be tagged with any character
last active file in directory is remembered if returning to dir visited before
dir shows no. of files it contains, or disk usage with key press 'dc', and disk usage of selected files shown at lower right
file permissions, owner, group and modified date displayed at lower left
disk free space shown at lower right
file transfer progress bar
file permissions easily changed on selected files by typing an octal number followed by '='
Ranger's file type executor "Rifle" opens files with a choice of programs, just hit 'r' and choose, or the Rifle number and Enter
audio and video played with mplayer, images with feh
scripts and make files can be run with Enter, thanks to rifle
almost limitless key mapping for easy commands in current dir, first key displays choices, second (or third..) key executes command or brings up console withwhich to modify the command, such as for renaming, replacing, or confirming with Enter
run native ranger commands in "console" or normal shell commands (though not all are possible) while using ranger's macros which can insert file name or path into the commands
press Up in the console to cycle the commands history
supports multple tabs and transferring files to next tab is also possible
supports hierarchal view of current dir location, showing parent/child dir's (default), or tabs side-by-side (NEW - 2016-12)
supports version control systems, for any specific dir, showing up-to-date with remote or modified status of files
attach metadata to files, such as title, author, and sort files by their metadata
clicking on parent dir in title bar moves there, Shift-click on title bar selects the path
And that's just the basics! There are endless possibilities with Ranger because it incorporates the bash shell.
move up 10 lines (default, I change it to 4), or move up 10 lines {n} times
k, {n} k
move down 10 lines, or move down 10 lines {n} times
g {letter}
quick go to (system level dir's) - gR go to ranger dir
g n
new tab made at home
Ctrl-n
new tab opened at current dir
tab, shift-tab
switch tab
q
quit tab or ranger if only one tab
u q
restore closed tab
[ or {n}[
move up 1 dir in same parent, or move up n parent dir's
] or {n}]
move down 1 dir in same parent, or move down n parent dir's
H
move back 1 dir in history (I make this Alt-left)
L
move forward 1 dir in history (I make this Alt-right)
m {x}
bookmark the dir x (any letter/digit)
`{x} or '{x}
go to dir bookmarked x
``
go to previous dir = effectively you can use `` to go back and forth between two locations
pg up, pg dn, home, end
move down/up file list
d c
update cumulative dir size
d u
show dir content summary
J
escape Ranger from reading a heavy dir/file
sorting
o t
sort by type first
o s
sort by size first
o a /o c /o m
sort by time
o n
sort natural
o {T/S/A/C/M}
sort in reverse order
o r
reverse the sort order
file actions
/, f
enter file name to navigate (f can't do this with my keybinds as I make use of it)
n
search newest file in dir, or next after search with /, f
N
previous file after search
c..
set the next search order
o..
set the sort order
i
focus a highlighted file with less (but only able to see part of file)
i to toggle focus, E to edit in nano
y y
copy
y a
copy add to buffer (can copy-paste files from multiple directories this way)
y r
copy remove from buffer
y G
copy all files from selected to bottom of list
y gg
copy all files from selected to top of list
d d
cut (da, dr also to cut from multiple directories, dG, dgg also)
p p
paste
p P
paste (add to queue)
p o
paste with over-write
p O
paste with over-write (add to queue)
u d
clears copy/cut buffer, also uy (I change this to uu as it seems faster)
c w
rename/bulk rename
I
edit name, cursor at front (I change it to i, and make Alt-i view in pager)
A
edit name, cursor at end
a
edit name, cursor inside extension
E
edit file with bash editor (I change this to fe - there is F4 as well - see how to set nano as editor below)
insert
touch (create new file) (I add ft)
delete
deletes file (no confirm for single files by default -can change in conf.rc)
r
open file with program on rifle list
r 1
unpacks selected archive (need to install atool) -actually hits the 1st option with rifle (you can just type 1 Enter)
11,12,13,14 Enter
(on image file) set image as desktop background with feh (11=scale, 12=tile, 13-center, 14=fill)
+ / -
chmod commands
{octal no} =
change permissions, e.g. 744 =, set permissions to -rwxr--r--
marking
Space
mark
v
invert selection
V, uV
marks/unmarks files as cursor moves, until escape pressed, or yanked etc (I change this to b)
u v
unmark all files
t
tag toggle
u t
remove all tags
T
remove any tags of selected files
"{x}
custom tag
M..
linemode select (I change this to Alt-m)
operation
Ctrl-R
reload
Ctrl-h, z h
toggle hidden files (I add to this F2)
Ctrl-c
cancel an operation
Ctrl-s
freeze ranger! Ctrl-c to continue
:flat 1
puts all files of sub-dir's (1 level deep) in the main column
z
toggle settings
:, ;
open console to enter commands
s, !, @, #
open shell console
1?
display all key bindings alphabetically
2?
Ranger commands usage
3?
Ranger settings
?, F1
man page, key bindings or settings
W
message log
w
job queue
Macros used in console and shell
%f
the highlighted file
%d
the path of the current directory
%s
the selected files in the current directory. (defaults to highlighted file if none selected)
%t
all tagged files in the current directory
%c
the full paths of the currently copied/cut files
%p
the full paths of the selected files
Flags used with open_with and shell commands
(type r for open_with, or s, !, @ for shell. open_with flags must be plain with no dash -)
-f
runs process in background - useful for an encoding/mogrify job as ranger will still be usable while the process completes
-c
runs on current file, not selected
-r
runs with super user privileges
-t
will show the process in a new terminal window (opens xterm)
Flags used with only the shell command
-p
sends the process to the pager - which is closed with q, or canceled before with Ctrl-C
-s
silent mode - the output is discarded
-w
bash waits at the end of the process so that you can read the output and then press enter
typing '@-rf geany' will open a file with sudo (writes `shell -rf geany %s` to console)
Check the rc.conf file
My keybindings
Updated Aug 2018
keybinds I changed from the default..
Note: with Ranger 1.7 key mapping with Alt is (almost) fixed, e.g. <a-r>, which opens up a huge area for new keys! Still can't use <a-[> or <a-bracketleft> or <c-1>
f2 script, duplicates file, puts suffix inside the file extension:
"f3" script, to rename the highlighted file with the selected file's name, keeping original extension
map O to open files
map y,d,p for yank, cut and paste commands
map P for commands with super user privelege
map X for delete commands
map dot for finding config files
map '; for cd to config dir's
map comma-k for backup commands/scripts
script for ,kp (as Ranger chokes on the date-time macros thinking they are its own)
(the k2 and k9 scripts are simple rsync commands - see my config migrate page)
map comma-l for symlink commands
map comma-d for directory commands
map comma-a for archive commands
map comma-i for iso/dvd commands
map comma-s for source file commands (though dropping to shell might be more advisable)
map F for feh image viewer commands
map fi for image file commands
map fim for mogrify commands
map E for exiftool and jhead commands
script for Ec, "exif-comment"
script for Ef, "exif-sort"
script for fD, "sort-files" (a very useful one to have when you've imported 400 clips from a video camera etc - no EXIF data)
map fv for video file commands
scripts tor ffmpeg conversion of flac to ogg/mp3 - but see audio file commands for oggenc
"flac-ogg"
"flac-mp3"
map fa for audio edit commands
map fG for GPS Babel conversion
map comma-m and comma-u for mounting/unmouning drives
There's a pmount plugin written by vifon which uses Alt-m {x} {n} to mount, but my pumount %f has got to be faster for unmounting
Here's my complete Ranger renaming tool that just might take care of all your needs for naming files!
You might want to have these printed on a sheet because some commands are too long and a #comment will not be seen. You should also run some tests on dummy files - don't assume that they all work perfectly!!!
script for append file modified date (Ra3)
note the use of the parameter $@ which will hit all the files passed to bash script, not all the files in dir
script for image date prepend (Re1)
script for image date append (Re2)
script for append numbering (Ra4)
i=1 start from, 3d =digits, +1 =step
script for renaming images with date-time (Re3)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------
One problem is, however, with so many key-bindings, is the lack of sort-by-key in the command visual. They are sorted alphabetically by command, and so finding the one you want........ I split the renaming up so as to not fill up R with a huge mix up of commands.
Notes on mapping keys
console is used when typing input is necessary, or the mapped command can be altered, such as a pattern or new file name
shell must be used in order to run bash commands, and console shell where typing is needed (can use ranger macros and the shell flags)
chain allows multiple commands to be run consecutively, each separated by `;`
without using 'console' a key-mapped command will execute immediately, without needing confirmation (in some cases it could be dangerous!)
using %s in the mapping may not be a good idea in some cases, i.e. when the console is opened before executing, because it will expand to all the selected file names, and that would 1. render the console full and impossible to type in, and 2. send utter garbage to a shell command - so we need to use %%s in the key mapping, which remains "%s" on the console, and then shell commands can act on every file.
Just make sure that if you change a mapping and remove "console" to make it key-executable (only with "shell") then you need to remove the "%%" (i.e. make it "%s" or "%f") or the command will fail.
console -p{n} will set the cursor at the nth character in the console, to make it convenient to edit a command, i.e. 'console -p22' will set the cursor at char 22
%space is used to ensure a space is inserted after a command, to avoid making uknown commands when space is not put in, e.g. 'console rename%space'
Useful native commands
search_inc
map / console search_inc (space after)
which searches as you type, but no filtering like scout
scout
map / console scout -ilt (space after)
hit / and type pattern, searches as you type, missing letters out, can correct your search until right file is selected
scout options as listed in commands.py:
-a
automatically open a file on unambiguous match
-e
open file on hitting Enter
-f
reduces file list to scout hits, (same as filter command does)
-g
interpret pattern as a glob pattern
-i
ignore case
-k
keep the console open when changing a directory with the command
-l
letter skipping; e.g. allow "rdme" to match the file "readme"
-m
mark the matching files on hitting Enter
-M
unmark the matching files on hitting enter
-p
permanent filter on hitting Enter (unmatched files stay hidden)
-s
smart case: lower case hits all, uppercase hits only uppercase
-t
searches as you type
-v
inverts the match
ready-made Ranger aliases for the `scout` command
alias filter
scout -iprt
alias find
scout -aet
alias mark
scout -mr
alias unmark
scout -Mr
alias search
scout -rts
alias travel
scout -aefikst
filter
map | console filter%space
followed by any string, filters file names, including extensions, case-sensitive
travel
map \[ travel%space
pretty much does `scout -efklt ` which will navigate into the dir filtered by typing part of a dir name, when Enter is pressed, and the console is kept open to allow you to search for the next dir to enter
enter_bookmark
map <a-a> enter_bookmark a
will map Alt-a to go to bookmark a (same as using the ' with its list of bookmarks but more permanent)
find tagged files
quickly go to tagged files in dir with ct and n
use the %t macro to execute a command on all tagged files in dir
show the "tagged" file with, e.g.
map f" shell -p less ~/.config/ranger/tagged
set nano as editor
add this line to the end of your ~/.bashrc file
Adjusting rifle
To change how different files types are opened (or the numbered choices when you hit 'r') you need to look in Ranger's rifle.conf file
Open images in feh, fullscreen, slideshow with progress to next, change the line to this,
Change how audio files open, in a new terminal window
Opening office docs, set the version you are using, e.g.
In-line preview of images
To have images showing in the pager at right of file list, first install w3m w3m-img
then enable image preview "set preview_images true" in conf.rc
To show image preview for videos, you must first install ffmpegthumbnailer and you need to uncomment the two lines starting "###video/*)" in scope.sh (and make sure you copy the Ranger 1.7 scope file to ranger config dir)
This only works with rxvt, rxvt-unicode, and xterm terminals - so I advise switching to URxvt. See my Rxvt-unicode page
Refine the info displayed for image files
To refine the info shown in pager for image files (because otherwise there's way too much info there and it runs off the screen and you don't want to hit `i` every time you need a useful exif data), add this line to scope.sh in ~/.config/ranger, for image mimetype:
Note, this is the newer version, for the 2016-12 Ranger git master
Refine the info displayed for video/audio files
I have mediainfo commands that can go in scope.sh file as well, and if you want to add more tags to the commands, you need to run
to put all the available tags into a file - and read that file to see what you can use - test them with:
mediainfo --inform="group;%tag%" file
mediainfo commands will print any extra characters placed outside the `%tag%` in the output, so I add some to denote tabs (>) and line breaks (^) and pipe the output to sed which converts them to real tab spaces and line breaks before sending to pager, so that the info that mediainfo returns from a file will be displayed nicely.. now that's clever :^)
Note, this is the newer version again..
(this all goes inside the mimetype "case" section, between lines "case" and "esac")
Note that "File_Modified_Date_Local" shows the date in local time, but "File_Modified_Date" shows it in UTC (GMT).
If certain file extensions don't show any info (eg ogg previously with ranger 1.6), you need to add a new case section:
then you hit 'zi' (`set preview_images!` as of 2016-12, or my zx key) to switch from preview to details - though you need to refresh the dir with Ctrl-r :(
empty the trashcan(s). use flag --shred to wipe it
trash-list
list trashed file
restore-trash
restore a trashed file (trashed from the current dir)
trash-rm
remove individual files from trash can
trash-empty 7
empties files trashed more than 7 days ago
with ranger, map the delete key - map shell -s trash-put %s
works a charm!
bookmark ~/.local/share/Trash/files to easily retrieve files
or you could map keys to the trash places for each disk partition, use `shell cd`
then to restore a file, select and yank with yy, then hit h (I change history key to lower case h from H) to return to the dir you just deleted the file, and pp to drop the file back and restore it there.
UI enhancements!
1. Remove "user@host" from the title bar
in ranger type `g R`
navigate to gui/widgets/ and highlight titlebar.py
type exactly: `@ gksu geany`
or if you have geany/editor mapped to a key, e.g. my `shell -f gksu geany %s` is mapped to Ps
in the file comment these lines (#), then save and quit/restart ranger
or in ranger type `gR`, `yy` on colorschemes and `pp` in ~/.config/ranger/
edit the color scheme default (backup first)
under "context.titlebar"
indent must be exact
change white in tab??
Mounting and browsing an android device
1. using SSH and the phone's wifi hotspot
get "SSHFS Droid" app
in the app settings: unselect require wifi, change p/w
start the ssh server with the app, and run the wifi hotspot
connect to the android hotspot on the linux box
make a directory to mount the device on with, e.g.
enable allow_other in /etc/fuse.conf
mount the android device with the command (noting the ip address that SSHFS Droid gives after linux box connects to the hotspot - in my case 192.168.43.1:2222) - this will vary every time if mobile data is turned on! (so turn it off)
now the device will be mounted at /mnt/phone, with /storage (i.e. SDcard, extSDcard) at top level
normal sshfs unmount is with
But Ctrl-C in the active sshfs window works to unmount ok.
Ranger mapping:
2. using mtp connection via usb cable
install mtp
make a dir in user home for a mount point
then connect the phone via usb and set file transfer to mtp
run `go-mtpfs ~/mount-point-dir`
unmount the device with `fusermount -u ~/mount-point-dir`
The following commands are used for checking the android's connection, and may not be necessary for connecting, but if the device will not mount, try detecting it first:
Ranger mapping:
Got there! Now go ahead and enjoy some real file management!
Next page:
Firewall - setting the network traffic firewall using iptables