my fav cli utils pt 2 - ranger file explorer


In writing this blog post (jk I won't link it, it's the one before this one, just keep reading this) about the howdoi cli util, I referenced another util called ranger. It's a file explorer that has vim like navigation and is great for quickly browsing through files in a way a hundred times faster than a familiar flow:

  1. going to github repo
  2. clicking on src folder
  3. guessing where the guts are and not where there's random stuff
  4. finding it but then having to navigate to something referenced with the back button etc and losing the initial page of interest.
  5. not remembering why I even ended up in this repo

Download it with brew install ranger. There may be python issues. I am not responsible if you mess up your $PATH. Jk that is very much fedora behavior. If you do or python is all messed up, factory reset and follow... a guide I'll mention later so you don't click out (and I would never open a new tab for you (and I say this as someone who cannot read an article without the cost of 10 new tabs))

# what it looks like when u use it

cd into a directory, or honestly just type ranger into ur term. So, the beauty of this, is imagine opening your /Users/myLilUser dir in your text editor (it's VSCode, we all know it, unless it's vim, which stop reading at this point if it is), and then browsing around by expanding a ton of different directories and clicking on a bunch of different files, which open new file tabs (unless you have that one setting enabled) and then you find some file of interest and then want to go back to a previous file of interest and now you have 80 file tabs open and can't find it ET CETERA. Not to mention your machine's fans would def be red lining.

With ranger you can open the dir, any dir, and not have to worry about performance or speed or any of that. It's BLAZING FAST (insert fire emoji) (can we stop using that).

It is as fast as your key repeat settings are (defaults write -g ApplePressAndHoldEnabled -bool false (do this if you haven't done it already (now it looks like I'm writing (LISP)))).

You move around using vim keybindings or emacs whatever that is 😛. If you don't know vim then pretend you're playing some 2d game, and up down left right is ikjl respectively. To get out of it... well you probably already know that from getting out of a commit merge and panic:Qing.

:search <query> allows you to search the dir. e to edit the file, i is for full screen, :q to quit ;~).

# internals

Tangent: as is evidenced by this post and my one on howdoi I love to poke around the internals of #rando cli utils. I always end up going to /usr/local/bin/the_util and then am like where do I find the guts and the answer is brew info the_util:

The code is incredibly well documented. The gist is that it uses os.walk to browse files and relies on whatever terminal you run ranger in for display. You should checkout ranger/doc/config/commands.py if this stuff interests you. For example here is the annotation for a the chmod fn.

class chmod(Command):
    """:chmod <octal number>

    Sets the permissions of the selection to the octal number.

    The octal number is between 0 and 777. The digits specify the
    permissions for the user, the group and others.

    A 1 permits execution, a 2 permits writing, a 4 permits reading.
    Add those numbers to combine them. So a 7 permits everything.
    """

All of the commands are basically just wrappers around python's os lib, for example, chmoding something happens like so:

def execute(self):
    mode_str = self.rest(1)
    if not mode_str:
        if self.quantifier is None:
            self.fm.notify("Syntax: chmod <octal number> "
                           "or specify a quantifier", bad=True)
            return
        mode_str = str(self.quantifier)

    try:
        mode = int(mode_str, 8)
        if mode < 0 or mode > 0o777:
            raise ValueError
    except ValueError:
        self.fm.notify("Need an octal number between 0 and 777!", bad=True)
        return

    for fobj in self.fm.thistab.get_selection():
        try:
            os.chmod(fobj.path, mode)
        except OSError as ex:
            self.fm.notify(ex)

    # reloading directory.  maybe its better to reload the selected
    # files only.
    self.fm.thisdir.content_outdated = True

# rc

There is an extensive rc file with innumerable modifications for things like what type of image file viewer you want to use, what browser to open links in, default editor, and more. It's 700 lines long and also very well documented. It's rc.conf in the repo. You can just copy it and paste it in ~/.config/ranger/rc.conf which is where ranger looks for user settings.

# that python article i mentioned

This entire guide (opens new window) is amazing but I always refer back to the python section when I have somehow unknowingly messed it up on my machine.

Here is a cheat sheet for ranger commands which you can just kinda guess if you've used vim:

And here's a vim overview I wrote.