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

Run vim commands inside yazi.nvim #547

Closed
sohanemon opened this issue Nov 4, 2024 · 8 comments · Fixed by #575
Closed

Run vim commands inside yazi.nvim #547

sohanemon opened this issue Nov 4, 2024 · 8 comments · Fixed by #575

Comments

@sohanemon
Copy link

This command functions effectively in Neovim when yazi.nvim is not open. Is there a method to execute this command while yazi.nvim is open, or to achieve similar functionality using a different key mapping/yazi plugin while yazi.nvim is open? Or can I use vim apis inside yazi plugins?

image

@mikavilpas
Copy link
Owner

Hmm I'm afraid right now there is no way (currently in use) to execute code in neovim. Sending commands from yazi to neovim is a bit problematic - I think we briefly discussed this a couple of days ago in some issue.

I think it might work in principle by making yazi run a shell command, which would tell neovim to execute some lua code.

When yazi.nvim runs yazi, it automatically gets an environment variable NVIM which is a socket file connected to the parent neovim process. It can be used to communicate with neovim, so technically all remote code execution is always available.

However, this has not been done before, although some point I would like to have a yazi side plugin ("nvim.yazi") that can offer deeper integration with yazi and remove these technical restrictions.

If you are willing to have an nvim-only keymapping, you could add one similar to <c-\> here
image

@mikavilpas
Copy link
Owner

I also have another idea that might work for your use case 🙂

hold on a bit

mikavilpas added a commit that referenced this issue Nov 4, 2024
Expose the current working directory as `NVIM_CWD` so that yazi
keybindings can use it to offer basic neovim specific functionality when
running yazi inside neovim with yazi.nvim.

Reference: #547
mikavilpas added a commit that referenced this issue Nov 4, 2024
Expose the current working directory as `NVIM_CWD` so that yazi
keybindings can use it to offer basic neovim specific functionality when
running yazi inside neovim with yazi.nvim.

Reference: #547
mikavilpas added a commit that referenced this issue Nov 4, 2024
Expose the current working directory as `NVIM_CWD` so that yazi
keybindings can use it to offer basic neovim specific functionality when
running yazi inside neovim with yazi.nvim.

Reference: #547
mikavilpas added a commit to mikavilpas/dotfiles that referenced this issue Nov 4, 2024
@mikavilpas
Copy link
Owner

Ok, I added a way to get the nvim current working directory to be available to yazi keymappings. I added an example of using it here in my own config feat(yazi): gr uses NVIM_CWD when available

@sxyazi
Copy link

sxyazi commented Nov 4, 2024

If it's just communication from Yazi ==> yazi.nvim, maybe DDS would be more convenient.

With a key press in Yazi, run a shell command:

{ on = '<C-t>', run = 'shell "ya pub my-message" --confirm' }

And if the user has registered an event for that my-message kind in yazi.nvim it will be triggered

@mikavilpas
Copy link
Owner

Oh that's a great idea - I can't believe I forgot about that possibility. I'll implement support for that later, I think. 👍🏻

@sohanemon
Copy link
Author

Ok, I added a way to get the nvim current working directory to be available to yazi keymappings. I added an example of using it here in my own config feat(yazi): gr uses NVIM_CWD when available

Thanks so much for adding that feature! It's really helpful to have the current working directory in Yazi key mappings. Great work!

mikavilpas added a commit that referenced this issue Nov 16, 2024
> note: DDS means "Data Distribution Service". It's a way for yazi to
> communicate with neovim and other processes.
>
> https://yazi-rs.github.io/docs/dds

Issue
=====

Yazi is incredibly versatile and can be used for many different things.
However, it's too difficult to predict what the needs of every user will
be. Power users have no easy way to extend the behaviour of yazi.nvim
beyond the features that are built-in.

Solution
========

Allow users to subscribe to custom yazi DDS events. These events are
possibly not included in yazi by default, but can be published by yazi
plugins or by yazi itself.

By default, all custom events are ignored. The user can specify which
events they want to listen to by setting a new `forwarded_dds_events`
(string[], default nil) config option.

When an event whose kind is included in this `forwarded_dds_events` list
is received, yazi.nvim will emit a neovim autocmd event with the name
`YaziDDSCustom`. The event will contain the `type` of the event and the
`raw_data` as a string. The user can then set up custom handlers for
these events in their neovim config.

Solves #547
mikavilpas added a commit that referenced this issue Nov 16, 2024
> note: DDS means "Data Distribution Service". It's a way for yazi to
> communicate with neovim and other processes.
>
> https://yazi-rs.github.io/docs/dds

Issue
=====

Yazi is incredibly versatile and can be used for many different things.
However, it's too difficult to predict what the needs of every user will
be. Power users have no easy way to extend the behaviour of yazi.nvim
beyond the features that are built-in.

Solution
========

Allow users to subscribe to custom yazi DDS events. These events are
possibly not included in yazi by default, but can be published by yazi
plugins or by other applications using yazi's DDS system.

By default, all custom events are ignored. The user can specify which
events they want to listen to by setting a new `forwarded_dds_events`
(string[], default nil) config option.

When an event whose kind is included in this `forwarded_dds_events` list
is received, yazi.nvim will emit a neovim autocmd event with the name
`YaziDDSCustom`. The event will contain the `type` of the event and the
`raw_data` as a string. The user can then set up custom handlers for
these events in their neovim config.

Solves #547
mikavilpas added a commit that referenced this issue Nov 16, 2024
> note: DDS means "Data Distribution Service". It's a way for yazi to
> communicate with neovim and other processes.
>
> https://yazi-rs.github.io/docs/dds

Issue
=====

Yazi is incredibly versatile and can be used for many different things.
However, it's too difficult to predict what the needs of every user will
be. Power users have no easy way to extend the behaviour of yazi.nvim
beyond the features that are built-in.

Solution
========

Allow users to subscribe to custom yazi DDS events. These events are
possibly not included in yazi by default, but can be published by yazi
plugins or by other applications using yazi's DDS system.

By default, all custom events are ignored. The user can specify which
events they want to listen to by setting a new `forwarded_dds_events`
(string[], default nil) config option.

When an event whose kind is included in this `forwarded_dds_events` list
is received, yazi.nvim will emit a neovim autocmd event with the name
`YaziDDSCustom`. The event will contain the `type` of the event and the
`raw_data` as a string. The user can then set up custom handlers for
these events in their neovim config.

Solves #547
@mikavilpas
Copy link
Owner

mikavilpas commented Nov 16, 2024

Ok, I implemented support for this today. I also implemented a pretty cool integration test setup for this, which I'm happy about 🙂

@sxyazi If you don't mind, I would like to ask a question for clarification. I could not get sending the event with ya pub MyMessageNoData to work as a yazi keybinding - only ya pub-to 0 ... works.

[[manager.prepend_keymap]]
on = "<C-e>"
run = """shell 'ya pub MyMessageNoData 2>&1 > ~/Desktop/output.txt' --confirm"""

Do you think I might have misunderstood something, or could this be a bug of some kind?

edit: the commands that worked can be seen here https://github.com/mikavilpas/yazi.nvim/blob/f8a1f1a5905e5a724ff90f2e2e7d11d246ac11d6/integration-tests/test-environment/.config/yazi/keymap.toml

@sxyazi
Copy link

sxyazi commented Nov 20, 2024

Ah, you're right, it should be ya pub-to instead of ya pub here. Sorry I forgot that yazi.nvim uses ya sub instead of yazi --local-events because of nvim's limitations.

ya pub is only used locally – that is, for the current Yazi process. When using ya sub, it acts as a separate client subscribing to all remote events, so ya pub-to is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants