Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: cd on exit #140

Closed
jocap opened this issue Feb 18, 2019 · 36 comments
Closed

Feature request: cd on exit #140

jocap opened this issue Feb 18, 2019 · 36 comments
Labels

Comments

@jocap
Copy link

jocap commented Feb 18, 2019

I think it would be nice to have the parent shell cd to the directory opened in lf on exit, similarly to what fff does.

There would be two components to this feature:

  1. Echoing the open directory to stdout on exit
  2. Creating a second command, 'cancel', which would exit with error without echoing the directory

(The 'cancel' command would make it possible to quit lf without having the shell cd to the opened directory.)

fff achieves step 1 by writing to a file on disk, but I don't this really is necessary. Instead of to stdout, the user interface can be printed directly to the tty, leaving room in stdout for echoing the directory.

Thoughts?

@occivink
Copy link
Contributor

occivink commented Feb 18, 2019

It's already possible, you can find some scripts (for posix shell or fish) in the repo for doing just that

@jocap
Copy link
Author

jocap commented Feb 19, 2019 via email

@gokcehan
Copy link
Owner

@jocap We have shell commands which pauses the ui and use the underlying stdin/stdout/stderr for input and output so things can get confusing if we use stdout instead of a separate file though I haven't actually tried this. We don't have a separate command implemented for step 2 but maybe someone may have already come up with a clever solution that I haven't considered. My workaround for this case is simply to go back with cd - after exiting if necessary.

@occivink
Copy link
Contributor

I think a solution would be for quit to take an optional return code, and in the wrapper script you would only cd if lf returned 0.

@gokcehan
Copy link
Owner

@occivink That could work I guess. We can also add a separate command for this. In the meantime, I also thought about a workaround using marks as follows:

push m-
map Q push '-q

@jocap You can put this in your lfrc file. First command marks the inital directory with - key, and the second command assigns Q key to load - mark first before quitting.

@jocap
Copy link
Author

jocap commented Feb 19, 2019 via email

@nostdm
Copy link

nostdm commented Apr 28, 2019

These solutions are pretty neat.

I added the following to my .zshrc:
I found it useful to name the function the same way as the lf binary and call the binary directly using the full path /usr/bin/lf:

lf () {
    tmp="$(mktemp)"
    /usr/bin/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
}

Also added this to .config/lfrc in order to use Q to return to the original directory:

push m-
map Q push '-q

Thanks!

@gokcehan
Copy link
Owner

@nostdm You can also use command lf instead of /usr/bin/lf so you don't have to hardcode the full path.

@Glench
Copy link

Glench commented Mar 18, 2020

Love that this feature is up for discussion. Any chance of it coming in a release?

@gokcehan
Copy link
Owner

@Glench The workaround already seems to work so I don't think there is anything else we need to do about this. We should probably close this issue.

@Glench
Copy link

Glench commented Mar 18, 2020

So the workaround is this?

map Q push '-q

and it lets you execute the cd command to the current directory displayed in lf? If so, where is the lfrc file go?

@gokcehan
Copy link
Owner

@Glench You also need push m- command so it saves the current directory in - mark on startup. Please see the documentation about where the lfrc file go.

@Glench
Copy link

Glench commented Mar 18, 2020

Oh sorry, mispaste. But even with that I don't see the expected behavior of leaving my shell cded to the last directory I was in in lf when I do Q. In fact, it says mark-load: no such mark.

$ cat ~/.config/lf/lfrc
push m-
map Q push '-q

@gokcehan
Copy link
Owner

@Glench Just to be clear, if you want to change to the last directory, you need to be using the script provided in /etc/lfcd.sh. If you want to cancel changing directory when using lfcd you can use that mapping. I have tried it again and it is working as intended. If you're still having trouble feel free to leave further information.

@freed00m
Copy link

@gokcehan I get random promts for mark-save: on startup with that tweak

# For exiting in the original direcotry lf started from
push m-
map Q push '-q

They are appear randomly, most noticably while launching lf right after exiting one in the same shell.

@ranebrown
Copy link

I'm also having issues with the suggested mapping but it isn't related to mark-save:. When I press Q I get mark-load: no such mark even though I can see the - mark saved when just pressing '.

@Glench
Copy link

Glench commented Mar 25, 2020

okay, I got something working with the /etc/lfcd.sh script in this repo but it's not what I wanted.

To be clear, what I was hoping for was the normal behavior of the lf program but a keyboard shortcut that does the cd on exit. So I would type lf then go to a different directory. If I type q then it just quits as normal and doesn't change directory but if I type Q (or cd or something) then it quits and changes the current working directory.

Is this possible currently or a planned feature?

@gokcehan
Copy link
Owner

gokcehan commented Apr 1, 2020

@Glench You can play around with the mappings to get what you want. For example:

push m-
map q push '-q
map Q quit

You can also define commands for this purpose.

@freed00m @ranebrown I have been able to reproduce it once but I can't do it again. It seems like a random bug. I'm marking this issue as a bug in the meantime.

@gokcehan gokcehan added bug and removed enhancement labels Apr 1, 2020
@ranebrown
Copy link

Seems like it might be related to using -. Using a character like w seems to work. (I think map q push '-q creates a recursive mapping, didn't work for me at least). Using the below there is a delay when exiting using q for some reason.

push mw
map q push 'w:quit<enter>
map Q push :quit<enter>

@gokcehan
Copy link
Owner

gokcehan commented Apr 1, 2020

@ranebrown I wrote the above example from head head and you're right, there is a recursive mapping, so q should be as in your example. I don't think it is needed for Q though.

Regarding the bug, I still can't produce it using w mark so I'm not sure if it's related.

@gokcehan
Copy link
Owner

There was a race condition with push commands which was most likely the reason for this bug and it is fixed in r17 release. Closing this issue now. Feel free to report back if the issue persists.

@ranebrown
Copy link

Delay issues seems to be fixed. Thanks!

@Lodifice
Copy link

Lodifice commented Apr 7, 2022

@Glench You can play around with the mappings to get what you want. For example:

push m-
map q push '-q
map Q quit

This is currently broken, as marks are written to disk immediately (and appear to be automatically sourced after the marks file changes). So if I open two instances of lf from different directories, then, when I press q in the first one, it will change to the directory the second instance was launched in.

@ilyagr
Copy link
Collaborator

ilyagr commented Aug 27, 2022

The push m- trick is very nice 90% of the time. Unfortunately, it doesn't work if you start lf with a path, i.e. lf some/other/directory. Then, pressing q would put you into some/other/directory, not where you started.

So, I think a better solution is still needed.

@ilyagr
Copy link
Collaborator

ilyagr commented Aug 27, 2022

I also tried replacing '- with cd $OLDPWD , but it doesn't work. cd $OLDPWD is an lf command, not a shell command, so it doesn't see the environment variable.

On the bright side, $OLDPWD does point to the correct directory, I think. Also, I suppose entering cd - when this problem does occur is not that huge of a burden.

@ilyagr
Copy link
Collaborator

ilyagr commented Aug 27, 2022

I think I solved the puzzle:

map q %{{
  lf -remote "send $id :cd $OLDPWD; quit"
}}
map Q quit

@Lodifice
Copy link

Lodifice commented Aug 30, 2022

Good @ilyagr this also solves the problem I had when running multiple instances of lf simultaneously. In the meantime I worked around the problem by having lf write the directory the parent shell should cd into to a custom file descriptor, but now I can use --last-dir-path again.

@ranebrown
Copy link

Is there some additional step to have OLDPWD defined? I get this error: --: line 2: OLDPWD: unbound variable

@Lodifice
Copy link

Lodifice commented Sep 6, 2022

Is there some additional step to have OLDPWD defined? I get this error: --: line 2: OLDPWD: unbound variable

$OLDPWD is a shell variable, so make sure to use it in a shell context like @ilyagr.
What shell do you use? It works for me with Bash.

@ranebrown
Copy link

I use bash. It seems to be unset when a new shell is opened. Even if I ensure it is defined in the shell where I spawn lf it seems to not be set within lf. I'm guessing lf is using a fresh shell and not inheriting that variable.

@ilyagr
Copy link
Collaborator

ilyagr commented Sep 11, 2022

Perhaps you are using really old version of lf (before r22, where this seems to have been introduced), @ranebrown?

If you enter !echo $OLDPWD<enter> in lf, you should see the value of the shell variable.

@ranebrown
Copy link

I'm on version 27. I get the same unbound variable error with the echo.

@harishnkr
Copy link

harishnkr commented Sep 23, 2022

The workaround seems fine but you have to type lfcd instead of lf. Adding alias lf to lfcd creates an error

lf:2: maximum nested function level reached; increase FUNCNEST?

Maybe increasing FUNCNEST may fix the issue, but is there a workaround to this workaround?

The same error comes up when renaming lfcd to lf in the lfcd.sh script

@ilyagr
Copy link
Collaborator

ilyagr commented Sep 23, 2022

@harishnkr You are getting an issue with infinite recursion. To solve it, change the lf -last-dir-path=$tmp $argv line in lfcd.sh to command lf -last-dir-path=$tmp $argv or /path/to/lf -last-dir-path=$tmp $argv. Afterwards, either of your solutions should work.

@Lodifice
Copy link

Lodifice commented Oct 11, 2022 via email

@harishnkr
Copy link

@ilyagr Thanks! It worked without a problem.

ilyagr added a commit to ilyagr/lf that referenced this issue Dec 22, 2022
People keep running into an infinite loop and being confused.
See gokcehan#140 (comment)
and gokcehan#1043.

This fixes the issues in `lfcd.sh` and `lfcd.fish`. For the other
scripts, I'm not sure whether this issue exists or is fixable.

One of the discussions above also suggested the need to document
the key binding better.
gokcehan pushed a commit that referenced this issue Feb 11, 2023
People keep running into an infinite loop and being confused.
See #140 (comment)
and #1043.

This fixes the issues in `lfcd.sh` and `lfcd.fish`. For the other
scripts, I'm not sure whether this issue exists or is fixable.

One of the discussions above also suggested the need to document
the key binding better.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants