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

clifm understands shell functions (e.g. zoxide in zsh) - feature request #200

Open
knodalyte opened this issue Mar 19, 2023 · 9 comments
Open
Labels
improvement New feature or request suggestion

Comments

@knodalyte
Copy link

Is your feature request related to a problem? Please describe.
Is is possible to either set something in the config file(s) or modify how clifm interoperates with my shell (zsh in my case) so that I can use shell functions from within clifm? For example, I would prefer to use zoxide to change directories in both zsh and in clifm, so that I don't have separate directory histories in each.

Describe the solution you'd like
For example, having configured zoxide in my .zshrc, in clifm I could use the z command to change directories, similar to using the existing clifm j command.

Describe alternatives you've considered
Use z in shell and j in clifm

Additional context
If this can be supported, I suspect other shell functions will be usable (nothing in mind right now...)

@knodalyte knodalyte changed the title clifm understands shell functions (e.g. zoxide in zsh) clifm understands shell functions (e.g. zoxide in zsh) - feature request Mar 19, 2023
@leo-arch
Copy link
Owner

Hi @knodalyte!

I guess it's possible, everything is it, by I'm not sure about the implementation details. For the time being, the first thing coming to my mind is a plugin: nnn has a plugin for several dir jumpers (including zoxide), which I think could be easily adapted to clifm.

Let me take a look at it. I will try the plugin approach first.

@leo-arch leo-arch added improvement New feature or request suggestion labels Mar 19, 2023
@leo-arch
Copy link
Owner

leo-arch commented Mar 20, 2023

#!/bin/sh

# Description: Jump to visited directories using external directory jumpers
# Author: L. Abramovich
# License: GPL3
#
# Dependencies: zoxide

if type zoxide >/dev/null 2>&1; then
        if type fzf >/dev/null 2>&1; then
                dir="$(zoxide query -i -- "$@")"
        else
                dir="$(zoxide query -- "$@")"
        fi
        [ -n "$dir" ] && echo "$dir" > "$CLIFM_BUS"
fi

exit 0

Hey @knodalyte, here's the first version of the plugin, for the time being supporting only zoxide.

Here's how to install the plugin:

  1. Copy it into $HOME/.config/clifm/plugins/autojump.sh.
  2. Add an action pointing to this plugin: actions edit and then add this line: z=autojump.sh. Save and exit the editor.
  3. Run z (plus any desired query). Example: z mydir.

This method has a few drawbacks however:

  1. Unlike the built-in j command, neither TAB completion nor autosuggestions are available.
  2. zoxide remains basically frozen while running in clifm: no new directories will be added and scores will be left untouched.

As a workaround for the second point, and I'm just thinking aloud, clifm's prompt commands might be used to update the zoxide database (using the zoxide add command) every time you change directories. I will explore this option and let you know.

@leo-arch
Copy link
Owner

leo-arch commented Mar 20, 2023

UPDATE
It works!

To keep the zoxide database up to date, add this prompt command to clifm's main config file (config or F10):

promptcmd zoxide add -- "$PWD"

NOTE: This is exactly the same thing zoxide does natively: it adds a zoxide add command to bash's PROMPT_COMMAND, which is executed immediately before each new prompt, to add new directories to its database.

@leo-arch
Copy link
Owner

UPDATE 2

There's still one thing that needs to be figured out: when running on a regular shell, zoxide keeps track of the last added directory to prevent adding it once and again whenever you issue a command (or just press Enter), generating thus a new prompt, but not changing the current directory. In this case, zoxide won't add anything to its database.

@leo-arch
Copy link
Owner

leo-arch commented Mar 20, 2023

UPDATE 3

To avoid adding the current directory once and again, replace the above promptcmd line

promptcmd zoxide add -- "$PWD"

by this one:

promptcmd [ "$(cat /tmp/z_oldpwd)" != "$PWD" ] && zoxide add -- "$PWD" && echo "$PWD" > /tmp/z_oldpwd

Sadly, we need to store the last added directory in a file (plus a command call: cat), which is sub-optimal. But, it basically works.

@knodalyte
Copy link
Author

Thank you for a quick solution!

As you pointed out, this more or less works, but it's the parts that don't work that provide most of the utility of zoxide.

I think I had unrealistic expectations about the extent the clifm command line would function like a commandline outside of clifm. Of course, the overhead of popping in and out of clifm is low enough that jumping back and forth is probably the best solution.

On a separate but related note, I would have expected that using the ; command would allow me to use shell functions like zoxide, so if my command was something like ;z proj but instead clifm tells me "sh: 1: z: not found".

@leo-arch
Copy link
Owner

leo-arch commented Mar 20, 2023

Thanks for you feedback @knodalyte!

As to the ; command, it will run whatever is in your PATH, preventing clifm from making built-in expansions. But shell functions, as far as I know, are not accessible outside the corresponding shell, not even from within a program launched from the shell itself (there's always a way though).

However, trying to make zoxide work within clifm (at least partially) totally worth it: it's useful for me to push clifm's capabilities to its limits, becoming aware thus about what it can do (even when I never thought about it), and what it cannot (at least now).

I think I will keep working on this plugin: it at least provides a nice integration with zoxide (as a viable alternative to the built-in j command), which I'm sure can also be achieved for other dir jumpers too.

@leo-arch
Copy link
Owner

Btw, which are exactly those parts that don't work? Are you referring to autosuggestions and TAB completion, or is it something else?

@knodalyte
Copy link
Author

Yes, exactly, as you pointed out, they are suggestions and tab completion.

I also found an old utility, xd, that is supposed to provide a quick way to change directories, you might want to see if it has any useful ideas for clifm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
improvement New feature or request suggestion
Projects
None yet
Development

No branches or pull requests

2 participants