about summary refs log tree commit diff stats
path: root/examples/shell_automatic_cd.sh
diff options
context:
space:
mode:
authorDmytro Meleshko <dmytro.meleshko@gmail.com>2019-11-02 15:21:20 +0200
committerDmytro Meleshko <dmytro.meleshko@gmail.com>2019-11-02 16:28:16 +0200
commit65721319e06789763798d0db1a7c24a08fee0bcd (patch)
tree7c1a5362cd3452bd2a6bad72043ef8d5baf6327d /examples/shell_automatic_cd.sh
parent9658e1127aa1b451b1a92d7d911b83b2c5c95d7d (diff)
downloadranger-65721319e06789763798d0db1a7c24a08fee0bcd.tar.gz
Renamed script files to not mention Bash
Diffstat (limited to 'examples/shell_automatic_cd.sh')
-rw-r--r--examples/shell_automatic_cd.sh22
1 files changed, 22 insertions, 0 deletions
diff --git a/examples/shell_automatic_cd.sh b/examples/shell_automatic_cd.sh
new file mode 100644
index 00000000..294f3aaa
--- /dev/null
+++ b/examples/shell_automatic_cd.sh
@@ -0,0 +1,22 @@
+#!/bin/sh
+
+# Compatible with ranger 1.4.2 through 1.7.*
+#
+# Automatically change the current working directory after closing ranger
+#
+# This is a shell function to automatically change the current working
+# 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.
+
+ranger_cd() {
+    temp_file="$(mktemp -t "ranger_cd.XXXXXXXXXX")"
+    ranger --choosedir="$temp_file" -- "${@:-$PWD}"
+    if chosen_dir="$(cat -- "$temp_file")" && [ -n "$chosen_dir" ] && [ "$chosen_dir" != "$PWD" ]; then
+        cd -- "$chosen_dir"
+    fi
+    rm -f -- "$temp_file"
+}
+
+# This binds Ctrl-O to ranger-cd:
+bind '"\C-o":"ranger-cd\C-m"'