Ranger configuration extras
Ranger is probably the single most powerful file manager application on any operating system.
It can be customized to the point of practically being a system-wide remote, one tool that can do anything. In this regard, it’s a very neat extension of the command line in Linux.
Because of that, almost every Ranger config file I’ve stumbled upon contains at least an interesting line or two. I’ve used the application for a few years already, but feel like there’s a lot that still needs attention. For example, a convenient way of dealing with archives of all sorts, or different file information views, depending on a particular need.
Below are some of my favorite additions to the rc.conf file. You can usually find it in ~/.config/ranger/ folder. If it’s not there, run this command in the command line: ranger –copy-config=all, which should create it.
Just paste the quoted lines wherever you see fit inside that file, but keep in mind that some of those commands invoke programs you will have to install.
Keyboard layout change
Using alternative keyboard layouts can be very painful for that person’s friends and family. If they sometimes need to operate on this person’s machine, after each session they might hate that person a little bit more.
Dvorak and Colemak are the unsung heroes of filesystem encryption, making all attempts to search YouTube or find something on Google fruitless, unless someone’s brain knows the layout. Even with keycaps being changed accordingly.
In order to help those poor people, we can set up an easy keyboard layout change to and from the basic QWERTY US layout to any we want. For that, we need to install setxkbmap with our package manager. For Ubuntu users, the command to paste in the command line is:
sudo apt-get install setxkbmap
Then, insert these two lines to rc.conf:
map Ku shell setxkbmap -variant ,us
map Kc shell setxkbmap -variant colemak
‘Ku’ and “Kc’ are my key combinations. You can set them to whatever you have unmapped. Then you need to restart Ranger, and whenever you want to change the layout, you just use those key combinations.
MP3 file tag preview
Sometimes, it is convenient to check the content of an MP3 tag. For example, a track we have on the hard drive might just have the artist and title in its filename, but we want to quickly check from which album it is or which year it was released.
Opening a browser for a Rate Your Music/Discogs/etc. check might take longer than getting to our music directory, quickly locating that file and pressing a key to see its tag contents.
For that, we’re going to need a CLI tag viewer. I highly recommend eyeD3, available both as a CLI app and an importable Python library. So, first in the command line:
sudo apt install eyed3
And in Ranger config file:
map T shell clear; eyeD3 %s; read -p “———-press ENTER to continue———–”
We are making a 3-step operation here:
- Clear the terminal window, so that tag information can be the only thing displayed, so we don’t have to strain our eyes looking for it.
- Pass the selected file(s) to eyeD3 for tag preview.
- Use read to display some information.
We need step 3, read with -p option (prompt). It waits for user input, enabling us to view the tag before coming back to Ranger.
Otherwise, we’d have to quit Ranger, display the tag, and start it again manually. Without the -p option, Ranger would display tag information and then instantly come back, requiring photographic memory coupled with Spiderman instincts to pick up the information we want. It would only be visible for a fraction of a second.
Notice that you can select multiple desired files, just like you normally mark them for operations in Ranger, and for each of them, tag information will be displayed in vertical order. Very handy.
MP3 player playback control
In my opinion, it is much better to have system-wide playback control shortcuts than narrowing it down to a specific application. This way, we can change tracks while the monitor is off and never have to worry about what’s focused, even if we spend the majority of time in our file manager.
If you don’t have any free system-wide available key combinations for that purpose, you can let Ranger do the ordering. As long as your music player accepts playback commands from a terminal session (if it doesn’t, you need to change your music player).
Ordering the player to start playback of hovered/selected file(s) is as simple as setting up your default music application in Ranger’s rifle.conf file. But maybe we want to enqueue instead of play?
In this case, we have to check what playback control commands a particular player accepts. Most applications will give such information when starting up from a terminal window with -h (short for ‘help’) added at the end.
In DeadBeef, enqueuing is done with the –queue parameter. The entire command looks like this:
map de chain shell deadbeef –queue %s; mark_files all=True val=False
You need to replace it with your player of choice and its equivalent command (which might very well be exactly the same).
The second part is also important. Ranger has a nasty habit of leaving selections after operations and no way to change that behavior, as far as I know. There is an option to drop all selections when leaving a folder, but it doesn’t work in my version.
It is not a problem though because we can slap the above mark_files command whenever we want to unselect after an operation and skip it whenever we don’t, giving us full control.
Starting Twitch streams
youtube-dl is a great tool to download YouTube videos, but it can also be used in concert with MPV to play its videos or playback Twitch streams.
For that, we of course first need to have both MPV and youtube-dl installed. Then, it’s as simple as adding one line for each stream to rc.conf:
map qan shell killall mpv; mpv https://www.twitch.tv/boxerpete </dev/null &>/dev/null &
The first part is optional and it kills all MPV instances. Since I want to play a stream now, first I want to end playback if something is already on, as I’m not going to watch two things at once.
Then I’m starting MPV with the Twitch streamer URL address. The player automatically invokes youtube-dl and begins stream playback.
In order not to freeze Ranger until stream playback is finished, which could be in 23 hours and 59 minutes in the case of a 24-hour stream, we need to order it to start as a separate process, hence the line ending.
Or are you like me and sometimes watch beautiful places on webcams while working, to cheat yourself that you’re not sitting inside a dark cave until the work is done?
map qge shell mpv https://www.youtube.com/watch?v=yMSc-qqW3To –no-audio –no-resume-playback </dev/null &>/dev/null &
Open with…
Default applications often don’t meet all our requirements for file handling. We might be using one application for the default action but also need another option for marginal cases.
Take images, for example. For casual users, the default action could be to open them in an image viewer. Sometimes, we might need to edit them, carve something out, and send it to someone? It’s good to prepare for such cases:
map wp shell pinta %s</dev/null &>/dev/null &
Pinta is a simple image editor for Linux. It is an equivalent of the old Paint Shop Pro on Windows, back before Corel bought them.
For simple modifications like slicing particular areas out of an image, there is no point in starting any graphical powerhouse, as we’d waste a lot of time looking at the loading screen while the program computes stuff we will not need.
Maybe I need to open a file in an editor I’m not using daily:
map us shell subl %s
Sublime Text spawns a separate process by default, so it doesn’t require a command ending to make it so.
Another use is to start a program with a specified geometry. I want to start the MPV player, but want it on my second 4:3 monitor:
map uv shell mpv –geometry=1280x1024+1920+0 %s</dev/null &>/dev/null &
We could add –fs before sending output to /dev/null to also start the program in full screen, but my experience is that it’s bugged and sometimes fullscreens on the first monitor, sometimes on the second.
Omitting it and specifying geometry is successful 100% of the time and there is no practical difference. It can be annoying if someone often click-drags the player window by accident (fullscreen prevents moving the window around). But in this case, it’s just better to disable it, with the exception of someone using a mouse and click-dragging the MPV window by accident. If that is your problem, just add this line to your mpv.conf:
no-window-dragging
Final word
Exploring the advanced configuration options for Ranger can transform the way you manage your files, bringing greater efficiency and personalization to your workflow. By implementing these extras, you not only make Ranger more aligned with your specific needs but also enhance your overall user experience. These tweaks and customizations are a testament to Ranger’s flexibility and capability as a file management tool.
I hope this guide has provided you with useful insights and practical steps to optimize your Ranger setup. If you have any questions or need further assistance, feel free to reach out.