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)?
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)?
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.
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'
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.
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.
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'
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
}
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.