Ranger cd into a folder (and invoke shell)?
Solution 1
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.
Solution 2
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'
Solution 3
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.
Solution 4
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.
Solution 5
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'
Related videos on Youtube
Philip Kirkbride
Updated on September 18, 2022Comments
-
Philip Kirkbride almost 2 years
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)? -
nilon almost 7 yearsand type
exit
in terminal to go back to ranger -
Philip Kirkbride over 6 years@Hi-Angel when you are browsing directories hit
S
it should leave ranger and go to a new terminal. But if you runexit
you go back to ranger. You need to use a capitalS
smalls
won't work. -
Enlico over 4 yearsThe link in your answer is broken, can you fix it, please?
-
laktak over 4 years@EnricoMariaDeAngelis updated
-
Rifaz Nahiyan about 4 yearsFor 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
-
flawr over 3 yearsThis is great, thanks for sharing!
-
NVaughan about 3 yearsGreat answer. Thanks.
-
Admin about 2 yearsAs 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.