33

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

35

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.

| improve this answer | |
  • 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
11

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.

| improve this answer | |
  • 1
    This does NOT invoke the shell in the specified folder. – gented Apr 4 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.

| improve this answer | |
  • The link in your answer is broken, can you fix it, please? – Enrico Maria De Angelis Jan 1 at 19:20
  • @EnricoMariaDeAngelis updated – laktak Jan 2 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 at 6:12
4

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.

| improve this answer | |
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'
| improve this answer | |

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.