summary refs log tree commit diff stats
path: root/examples/bash_automatic_cd.sh
diff options
context:
space:
mode:
authorhut <hut@lepus.uberspace.de>2015-04-13 12:49:49 +0200
committerhut <hut@lepus.uberspace.de>2015-04-13 12:49:49 +0200
commitc0f2fc72eccb4127fba5f48ce4b422487d6ec752 (patch)
tree025f72dde6f7a7e5cdca82637da4e5dcef0ddd62 /examples/bash_automatic_cd.sh
parenta9bbdc440c2ea33ccc4470e00949ffa16ce2300e (diff)
downloadranger-c0f2fc72eccb4127fba5f48ce4b422487d6ec752.tar.gz
moved "doc/examples" to "examples" for more visibility
Diffstat (limited to 'examples/bash_automatic_cd.sh')
-rw-r--r--examples/bash_automatic_cd.sh21
1 files changed, 21 insertions, 0 deletions
diff --git a/examples/bash_automatic_cd.sh b/examples/bash_automatic_cd.sh
new file mode 100644
index 00000000..465c9c80
--- /dev/null
+++ b/examples/bash_automatic_cd.sh
@@ -0,0 +1,21 @@
+# Compatible with ranger 1.4.2 through 1.6.*
+#
+# Automatically change the directory in bash after closing ranger
+#
+# This is a bash function for .bashrc to automatically change the directory to
+# the last visited one after ranger quits.
+# To undo the effect of this function, you can type "cd -" to return to the
+# original directory.
+
+function ranger-cd {
+    tempfile="$(mktemp)"
+    /usr/bin/ranger --choosedir="$tempfile" "${@:-$(pwd)}"
+    test -f "$tempfile" &&
+    if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then
+        cd -- "$(cat "$tempfile")"
+    fi
+    rm -f -- "$tempfile"
+}
+
+# This binds Ctrl-O to ranger-cd:
+bind '"\C-o":"ranger-cd\C-m"'