Preventing Archives From Freezing Ranger

Ranger is one of the few packages I simply can’t live without. Ranger is one of my most used CLI tools and my defacto standard for navigating directories and transferring files. If you love ranger like me though you have come to deal with its many bugs and excentricities. One of the most frustrating for me personally is rangers automatic previewing of archives. While this feature is great if you are dealing with nothing but tiny archives, but not in a directory full of +100GB archives… At that point ranger becomes unusable and a general nightmare.


To fix this issue, I wrote a small function to prevent automatic loading of archives larger than 512MB.

  1. First you need to create a set of user config files in ~/.config/ranger. This is done with the command:

    ranger --copy-config=all
    
  2. Next add the following function to the newly created ~/.config/ranger/scope.sh. This function checks if a given file is larger than 512MB.

    file_size_limit() {
    if [[ `du "${FILE_PATH}" | cut -f1` -gt 512000 ]]; then
        echo '----- FILE IS LARGER THAN 512MB -----'
        exit 0
    fi
    }
    
  3. This function can be called anywhere in scope.sh but to prevent ranger from auto-loading archives we will call it in handle_extension().

    handle_extension() {
    case "${FILE_EXTENSION_LOWER}" in
        # Archive
        a|ace|alz|arc|arj|bz|bz2|cab|cpio|deb|gz|jar|lha|lz|lzh|lzma|lzo|\
        rpm|rz|t7z|tar|tbz|tbz2|tgz|tlz|txz|tZ|tzo|war|xpi|xz|Z|zip)
            # limit file size           
            file_size_limit
            atool --list -- "${FILE_PATH}" && exit 5
            bsdtar --list --file "${FILE_PATH}" && exit 5
            exit 1;;
    

Now your terminal should look like this when you hover over archives larger than 512MB:

Loading Archives in Ranger

Avatar
Justin Timperio
Researcher and Freelance Dev

Justin is a freelance developer and private researcher.

Related