dotfiles/home/private_dot_config/lf/lfcd.sh

30 lines
645 B
Bash
Raw Permalink Normal View History

2021-12-08 09:38:24 -06:00
# Change working dir in shell to last dir in lf on exit (adapted from ranger).
#
# You need to either copy the content of this file to your shell rc file
# (e.g. ~/.bashrc) or source this file directly:
#
# LFCD="/path/to/lfcd.sh"
# if [ -f "$LFCD" ]; then
# source "$LFCD"
# fi
#
# You may also like to assign a key to this command:
#
# bind '"\C-o":"lfcd\C-m"' # bash
# bindkey -s '^o' 'lfcd\n' # zsh
#
2022-07-06 11:55:14 -05:00
lfcd() {
tmp="$(mktemp)"
lf -last-dir-path="$tmp" "$@"
if [ -f "$tmp" ]; then
dir="$(cat "$tmp")"
rm -f "$tmp"
if [ -d "$dir" ]; then
if [ "$dir" != "$(pwd)" ]; then
cd "$dir"
fi
fi
fi
2021-12-08 09:38:24 -06:00
}