与Gambai及其建议的其他变体相比,我在其他地方偶然发现了一个类似的问题,并且答案更好。更好。
- 它将通过将创建的文件放入tmp文件夹中来处理该文件,以便系统可以将其删除
- 它是更干净的代码(尽管Gambai的答案可以转换为函数)
护林员的git repo中的shell文件中已经有一个函数:
https://github.com/ranger/ranger/blob/master/examples/bash_automatic_cd.sh
function ranger-cd {
# create a temp file and store the name
tempfile="$(mktemp -t tmp.XXXXXX)"
# run ranger and ask it to output the last path into the
# temp file
ranger --choosedir="$tempfile" "${@:-$(pwd)}"
# if the temp file exists read and the content of the temp
# file was not equal to the current path
test -f "$tempfile" &&
if [ "$(cat -- "$tempfile")" != "$(echo -n `pwd`)" ]; then
# change directory to the path in the temp file
cd -- "$(cat "$tempfile")"
fi
# its not super necessary to have this line for deleting
# the temp file since Linux should handle it on the next
# boot
rm -f -- "$tempfile"
}
您可以将此函数放入您喜欢的shell rc(例如~/.zshrc
)文件中,然后创建别名和/或将其绑定到组合键(同样可以将其放入rc文件中):
alias nav=ranger-cd
和/或
# This will run the function by Ctrl+O through returning
# the string "ranger-cd" in addition to a new-line character
# to act as Enter key-press
bindkey -s "^o" "ranger-cd\n"
免责声明:在bindkey
中ZSH以上工作,你应该根据你的首选shell更改
;
,然后在分号后指定更多命令,我假设您在关闭时运行ranger
,谢谢!