gokcehan / lf

Terminal file manager
MIT License
7.79k stars 331 forks source link

Is it possible decide weather to the working dir AFTER enter `lf`? #1792

Closed slbb closed 3 months ago

slbb commented 3 months ago

According to the documentation, the user can output the last path before lf left via lf -print-last-dir. Then change the shell's working dir via a shell pipeline command. This requires the user to decide “whether or not to change the shell working dir” BEFORE entering lf, since the command to enter lf is different. However, there are cases where one can change one's mind after entering lf. The way lf handles working dir changes reminds me of another file browsing program, vifm. The behavior of vifm --choose-dir is similar to lf -print-last-dir. And vifm provides two exit commands. One is the normal :quit, which outputs the last-browsed dir to the shell; the other is :cquit, which aborts directory choosing and returns a non-zero exit code, thus can stop the shell from changing working dir. So is there any way for lf to achieve a similar result?

DusanLesan commented 3 months ago

I use this to quit to the old directory: map Q %{{ lf -remote "send $id :cd $OLDPWD; quit" }}

slbb commented 3 months ago

@DusanLesan Thanks for the information. That leads me to finally figure it out. In Windows powershell,

map Q &{{lf -remote "send $env:id cd $($env:OLDPWD.Replace('\','/'))"; lf -remote "send $env:id quit"}}

works for me.

Some information that might be useful to others: Place the line above in lfrc. Then if press Q to quit, lf will first go back to the directory where it started at, then quit. The syntax is depended on the shell. As for powershell, it should be valid powershell syntax and command. @DusanLesan's reply is the correct solution, but not in powershell syntax. &{{ }} quotation mark means async run shell command, and the sync way is ${{ }} quote. The first command lf -remote "send $env:id cd $($env:OLDPWD.Replace('\','/'))" let lf go back to the old directory. Note that under Windows powershell, the $env:OLDPWD is a path string in Windows backslash style (e.g. "C:\foo\bar"), which directly using with cd here will cause error like #1694. So need to do $env:OLDPWD.Replace('\','/') to convert it into Unix style. And quote this expression with $( ) when it's inside " " according to powershell syntax. The second command lf -remote "send $env:id quit" quits lf. $env:id and $env:OLDPWD are temporary environment variables exported by lf. Can be found via lf -doc command.