lf icon indicating copy to clipboard operation
lf copied to clipboard

ranger-style lfcd?

Open ianyepan opened this issue 4 years ago • 11 comments

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".

ianyepan avatar Mar 15 '22 06:03 ianyepan

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).

kmarius avatar Mar 15 '22 09:03 kmarius

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?

ianyepan avatar Mar 15 '22 16:03 ianyepan

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.

kmarius avatar Mar 15 '22 16:03 kmarius

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 avatar Mar 15 '22 16:03 ianyepan

@ianyepan The mapping map S $bash will start bash, simply change that to your shell.

kmarius avatar Mar 15 '22 16:03 kmarius

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 avatar Mar 16 '22 18:03 ianyepan

@ianyepan FYI, there's also already a default keybinding w for this purpose which is the character next to q on qwerty.

gokcehan avatar Mar 17 '22 19:03 gokcehan

@ianyepan FYI, there's also already a default keybinding w for this purpose which is the character next to q on qwerty.

Wait, but what is the purpose of the lfrc script then if you can do exactly the same by pressing w?

nikableh avatar Mar 26 '22 12:03 nikableh

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.

climbTheStairs avatar Mar 26 '22 22:03 climbTheStairs

lfrc, on the other hand

I believe you meant lfcd. lfrc is the config file's name.

ianyepan avatar Mar 27 '22 04:03 ianyepan

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.

Barbaross93 avatar May 22 '22 21:05 Barbaross93