Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to change directory in shell using Ranger

📅 2014-Sep-28 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ bash, cd, fish, ranger ⬩ 📚 Archive

One of the uses of Ranger can be to conveniently change to a directory in the shell that launched Ranger. To do this, we use the --choosedir option of Ranger. Pass this the name of a file and when Ranger is closed it will write the directory it was in to that file. You can then read the contents of this file in your shell to change to that location.

For Bash, a function that can do this is provided in in the Ranger source code.

For Fish, I have written a similar function that you can add to your ~/.config/fish/

# Add this to your ~/.config/fish/
function ranger-cd
    set tmpfile "/tmp/pwd-from-ranger"
    ranger --choosedir=$tmpfile $argv
    set rangerpwd (cat $tmpfile)
    if test "$PWD" != $rangerpwd
        cd $rangerpwd

To use Ranger like this, invoke it at the shell as ranger-cd. When you quit Ranger, fish will change its current directory to that in Ranger.

Tried with: Ranger 1.6.1, Fish 2.0.0 and Ubuntu 14.04