How to exit the Ranger file explorer back to command prompt but keep the current directory?

According to its manual

--choosedir=targetfile    
    Allows you to pick a directory with ranger. When you exit ranger, it will write the last visited directory into targetfile.

So all you need to do is create an alias like this:

alias ranger='ranger --choosedir=$HOME/.rangerdir; LASTDIR=`cat $HOME/.rangerdir`; cd "$LASTDIR"'

And writing this alias into the rc of your favoured shell is recommended.


Shift + S

If you hit Shift + S, it opens a new shell on the current directory.

Then if you hit Ctrl + D on the shell, it goes back to ranger.

This workaround is often good enough.

By the way, I've given up on file managers for a few years now, I just have this in my bashrc instead and I navigate directories simply with tab complete, it's good enough for me:

c() {
  if [ -n "$1" ]; then
    cd "$1" || return 1
  else
    cd ..
  fi
  ll
}
ll() ( ls -hl --time-style="+%Y-%m-%d_%H:%M:%S" "$@"; )

GitHub upstream.


I found an easier solution. When you install ranger, it will put a script in your bin folder which, if executed, will start the program. But if you source it, with

$ source ranger

it will launch ranger and drop you in the last visited folder when you exit.

so if you want this behavior by default, just do

$ alias ranger='source ranger'

or even better put it into your .bashrc file.

To see the documentation and implementation for this feature, read the ranger script in your bin folder.