ranger-style lfcd?
I've set up lfcd and it's working just fine. However, there are times when I don't know whether I'd like to change to a subdirectory at the time of invoking "lf", so I always have a mental overhead of deciding whether I should type "lf" or "lfcd" at the console.
Is there a way to have ranger-style lfcd? That is, invoke "lf", start browsing, and "later decide" whether I'd like to stay in this directory or go back to starting directory "upon quitting". In ranger, that's as simple as choosing between pressing "q" or pressing "S".
One way to achieve this is extending the lfcd function to check for an additional file and decide if it should navigate to the directory. In lf you would add an extra keybind or command that touches said file and quits (as compared to just quitting normally).
I see, so you're saying we need to "temporarily touch a file" as a workaround/hack for the quitting to end in the current directory (and later deleting the temp file)? If you have insight on how ranger achieved this, I'd love to know -- because intuitively one wouldn't think this is such a difficult task -- I wonder how ranger dealt with this and why lfcd needed these workarounds. There must be some fundamental differences between these two, right?
I don't think ranger even supports that. Per default S just opens a new shell at the current location, it does not exit ranger. Once the shell quits, you are dropped back into ranger. It is up to the wrapper to deal with this as lf and ranger only offer the option to (always) write the final location to some file.
EDIT: To clarify, neither ranger nor lf can change the directory of the shell which is exactly why the shell functions ranger-cd and lfcd are needed.
Per default S just opens a new shell at the current location, it does not exit ranger. Once the shell quits, you are dropped back into ranger.
Thanks for replying -- you're right. How would I go about achieving the same in lfcd? Ideally I'd like to bind "S" to create a new shell at the current directory, just like how ranger handles it, (unfortunately I'm not super familiar with shell scripting).
@ianyepan The mapping map S $bash will start bash, simply change that to your shell.
The mapping map S $bash will start bash, simply change that to your shell.
Thanks you. By putting this under ~/.config/lf/lfrc, I got the exact behavior I wanted! Closing issue.
@ianyepan FYI, there's also already a default keybinding w for this purpose which is the character next to q on qwerty.
@ianyepan FYI, there's also already a default keybinding
wfor this purpose which is the character next toqon qwerty.
Wait, but what is the purpose of the lfrc script then if you can do exactly the same by pressing w?
Wait, but what is the purpose of the lfrc script then if you can do exactly the same by pressing
w?
Pressing w only creates a new shell in the directory that lf is in, that when exited returns back to lf.
lfcd, on the other hand, changes to the directory lf is in when it quits.
Edit: Sorry, yes, that was what I meant. Thanks for the correction.
lfrc, on the other hand
I believe you meant lfcd. lfrc is the config file's name.
In case anyone else was trying to accomplish this, I ended up using the ranger function as inspiration to develop an equivalent function for lf.
lf () {
local tempfile="$(mktemp)"
command lf -command "map Q \$echo \$PWD >$tempfile; lf -remote \"send \$id quit\"" "$@"
if [[ -f "$tempfile" ]] && [[ "$(cat -- "$tempfile")" != "$(echo -n $(pwd))" ]]; then
cd -- "$(cat "$tempfile")" || return
fi
command rm -f -- "$tempfile" 2>/dev/null
}
Like the ranger function, starting lf will cause it to behave normally, i.e., quitting will return you to the directory you started the lf command. However, if you move to a new directory while in lf and then hit 'Q', you will exit lf into that directory.