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)?


7 Answers 7


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.

  • 9
    and type exit in terminal to go back to ranger
    – nilon
    Commented Jul 1, 2017 at 13:54
  • 1
    Doesn't work for me — typing s cd ~ still leaves me in /tmp directory.
    – Hi-Angel
    Commented Nov 29, 2017 at 6:29
  • 5
    @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. Commented Nov 29, 2017 at 12:24
  • 1
    As an alternative to exit, you can go back to ranger by pressing Ctrl-D (^D). That types the EOF character, telling the shell you will input no more commands. Commented May 21, 2022 at 14:22

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.

alias ranger='. ranger'
  • 5
    This is great, thanks for sharing!
    – flawr
    Commented Feb 12, 2021 at 13:23

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
    Commented Apr 4, 2020 at 13:55
  • But thanks! This is what I was looking for when I google brought me to this page.
    – mahela007
    Commented Sep 21, 2023 at 11:29

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=(
        --cmd="map Q chain shell echo %d > "$tempfile"; quitall"

    ${ranger_cmd[@]} "$@"
    if [[ -f "$tempfile" ]] && [[ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]]; then
        cd -- "$(cat "$tempfile")" || return
    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
    Commented Jan 1, 2020 at 19:20
  • @EnricoMariaDeAngelis updated
    – laktak
    Commented Jan 2, 2020 at 14:39
  • 2
    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 Commented Apr 2, 2020 at 6:12
  • @laktak Sadly this is not working with zoxide which I use instead of cd :(
    – Vignesh S
    Commented Oct 5, 2022 at 5:38

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:
        # same as quitall_bang (:quitall!)

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}" ]]
                cd "$(cat "${LOGFILE}")"
                rm -f "${LOGFILE}"

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

rangercd() {
    ranger --choosedir="$tmp" "$@"
    if [ -f "$tmp" ]; then
        dir="$(cat "$tmp")"
        rm -f "$tmp"
        if [ -d "$dir" ]; then
            if [ "$dir" != "$(pwd)" ]; then
                cd "$dir"
bindkey -s '^o' 'rangercd\n'
  • Great answer. Thanks.
    – NVaughan
    Commented Jun 21, 2021 at 16:24

If someone wants a python script to do the trick, check out the project ranger-quit_cd_wd. It uses a python script instead of shellscript to get the job done.

If you also use tmux, check out ranger_tmux for an integration solution.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .