40

I'm using Ranger to navigate around my file system.

Is there a shortcut where I cd into a folder without leaving Ranger (as in open bash with a location of a folder found by navigating in Ranger)?

41

I found the answer to this in the man pages:

S Open a shell in the current directory

Yes, probably should have read through that before asking here.

  • 4
    and type exit in terminal to go back to ranger – nilon Jul 1 '17 at 13:54
  • Doesn't work for me — typing s cd ~ still leaves me in /tmp directory. – Hi-Angel Nov 29 '17 at 6:29
  • 1
    @Hi-Angel when you are browsing directories hit S it should leave ranger and go to a new terminal. But if you run exit you go back to ranger. You need to use a capital S small s won't work. – Philip Kirkbride Nov 29 '17 at 12:24
12

You could also use :cd /path/to/folder if you are already in Ranger.

Update: The question has been edited since this answer was given, making it invalid.

  • 1
    This does NOT invoke the shell in the specified folder. – gented Apr 4 '20 at 13:55
9

You can also do it the other way and use ranger-cd to automatically change the directory in bash after closing ranger with this script.

function ranger-cd {
    local IFS=$'\t\n'
    local tempfile="$(mktemp -t tmp.XXXXXX)"
    local ranger_cmd=(
        command
        ranger
        --cmd="map Q chain shell echo %d > "$tempfile"; quitall"
    )

    ${ranger_cmd[@]} "$@"
    if [[ -f "$tempfile" ]] && [[ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]]; then
        cd -- "$(cat "$tempfile")" || return
    fi
    command rm -f -- "$tempfile" 2>/dev/null
}

Your shell changes the directory only when you quit ranger with keybinding capital Q (see map Q if you want to change this).

I use it with

alias r=ranger-cd

You can adapt this script to do other things as well, e.g. exit ranger and switch to a vim session in this directory.

  • The link in your answer is broken, can you fix it, please? – Enlico Jan 1 '20 at 19:20
  • @EnricoMariaDeAngelis updated – laktak Jan 2 '20 at 14:39
  • For those who are considering this trick, please also look at @Neurognostic's answer below too. Basically, you can achieve the same thing by sourcing ranger. Thus, running ranger this way works too: . ranger – Rifaz Nahiyan Apr 2 '20 at 6:12
7

Another approach is to have the underlying shell "follow" ranger(1) around the filesystem so that after navigating to a new directory and ranger(1) is quit (or suspended; usually ctrl+z) the underlying shell will already be in the same directory ranger(1) was quit in.

To do this, have the shell "source" ranger(1) either by prefixing the command with the word . (i.e., the dot or period character) or the word source on some shells.

. ranger

Now your shell will "follow" ranger(1) around the filesystem.

This works because the ranger command (which is python script) has an embedded bash(1) script that is read when sourcing the file. Note that, it only works on bash(1) compatible shells.

From a comment block in the script:

This embedded bash script can be executed by sourcing this file. It will cd to ranger's last location after you exit it. The first argument specifies the command to run ranger, the default is simply "ranger". (Not this file itself!) The other arguments are passed to ranger.

If this becomes your preferred mode to use ranger(1) in, add it as an alias in your shells initialization script.

0
rangercd () {
    tmp="$(mktemp)"
    ranger --choosedir="$tmp" "$@"
    if [ -f "$tmp" ]; then
        dir="$(cat "$tmp")"
        rm -f "$tmp"
        if [ -d "$dir" ]; then
            if [ "$dir" != "$(pwd)" ]; then
                cd "$dir"
            fi
        fi
    fi
}

bindkey -s '^o' 'rangercd\n'
0

My solution is less fancy, and involves a few more moving parts than the others here.

The idea is, when the user presses S (the typical binding for 'shell'), map that to a ranger command that writes the current directory to a temporary file.

Then, wrap ranger in a (in my case, zsh, but should work on bash as well) shell function, which checks if that temporary file exists. If it does, it cds to that directory, and then deletes that file.

Since shell functions execute in the current shell, they do change your current directory.

Though its a bit complicated than the other answers here, it doesn't leave the hanging ranger instance, and it works well.

So, in rc.conf, add the line:

map S quit_and_cd

In the commands.py (the file where you can create custom ranger commands) file, create that command:

import os
from ranger.api.commands import Command

class quit_and_cd(Command):
    def execute(self):
        with open("/tmp/cd_ranger", "w") as f:
            f.write(os.path.abspath(self.fm.thisdir.path))
        # same as quitall_bang (:quitall!)
        self.fm.exit()

Then, in your shell startup (.zshrc or .bashrc), create a function named ranger:

ranger () {
        local LOGFILE='/tmp/cd_ranger'
        # `command ranger` to launch ranger itself,
        # instead of causing an infinite loop with
        # this function calling itself
        command ranger "$@" || exit $?
        if [[ -f "${LOGFILE}" ]]
        then
                cd "$(cat "${LOGFILE}")"
                rm -f "${LOGFILE}"
        fi
}

I have these synced up to my dotfiles, see here and here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.