neovim
neovim copied to clipboard
API: `VisualChanged` autocmd
Feature description
For there to be a VisualChanged autocommand that fires whenever the visual selection changes. For v:event to be populated with visual mode and visual boundaries (row and col).
Alternatively, for there to be a visual_change ui event with the same information.
The information (mode and boundaries) is not strictly required, as it can be obtained by mode(), getpos('v'), and getpos('.'). However, getting it directly would be better. The most important thing is to be notified when the selection changes.
Background
I am the current maintainer of https://github.com/vscode-neovim/vscode-neovim.
Currently, visual selection is not properly managed. Instead of telling vscode exactly where the visual selection is (so that vscode can render it, use it for commands, show it in minimap, etc), we render it ourselves using the Visual highlight. This is suboptimal.
To properly sync visual selection to vscode, we need to know when the visual selection changes in neovim. We can't use the Visual highlight, because it only gives updates for ranges inside the viewport. We can't use CursorMoved, because the visual selection can change without the cursor moving (viw).
This is a duplicate of https://github.com/neovim/neovim/issues/11590.
I'm also interested in this new event, as I'm having the exact same issue in ShadowVim (Neovim embedded in Xcode).
At the moment I'm using ModeChanged and CursorMoved, which works in most cases. It fails when hitting viw if the cursor is at the end of the word, as none of these events are emitted.
@justinmk I don't really know how neovim development works/who is generally in charge of these types of changes, but in order to prioritise the simplification of vscode-neovim, what can be done to increase the priority of this issue (and others like it)? They seem quite straightforward to implement (for someone familiar with the stack), but significantly block progress. Thanks!
what can be done to increase the priority of this issue (and others like it)?
Mentioning that something unblocks an important use-case definitely increases the priority. But ultimately things get priority when we see a PR.
Mentioning that something unblocks an important use-case definitely increases the priority.
A second use-case would be synchronizing visual selections between two buffers.
I've been developing a small "assembly viewer". Two buffers, displayed in a vertical split, allow users to look at a source code line in Neovim and see the generated assembly line on the left. It's a WIP plugin but a gif should hopefully illustrate the functionality.
Right now the plug-in only highlights the assembly lines for the current line in Normal mode but it'd be great a visual selection could be accepted. And when changed, the assembly lines would show for the whole selected range. Changing the visual selection would change the highlighted assembly.
It might be doable without a VisualChanged event but would definitely be simpler with it.
To summarize: VisualChanged would be helpful to sync selections between 2+ buffers and this plug-in is another use-case.
Status
- Implementation was attempted in https://github.com/neovim/neovim/pull/26052 , but stalled and needs an owner.
- The
CursorMovedevent seems to trigger during visual-mode. So it may be possible for clients/plugins to implementVisualChangedby:- listening to
CursorMovedandWinScrolled - using the
getregionpos()function - ⚠️ caveat: doesn't capture when the visual selection can change without the cursor moving (viw at end of word).
- listening to
So it may be possible for clients/plugins to implement
VisualChangedby: - listening toCursorMovedandWinScrolled- using thegetregionpos()function
No, the visual selection can change without the cursor moving (viw at end of word).
but stalled and needs an owner.
Has there not been active development by the original author in the last week?
Has there not been active development by the original author in the last week?
I revisited the PR, but I'm not confident I can finish it
I asked this earlier in the PR but didn't see a response so asking here too - Is there no way to contribute VisualChanged to Vim and patch to Neovim? I think both communities would appreciate the change. Bringing it to Vim's attention also means having more people available to pick the work up, if needed. I'm personally interested and would be happy to try implementing it too if @BaraTudor2025 doesn't think they can finish it.
Edit: FWIW I think even just VisualChanged itself (and the current PR) is useful enough on its own. Additional data like v:event can always be added in follow-up work.
Edit: FWIW I think even just VisualChanged itself (and the current PR) is useful enough on its own. Additional data like
v:event
v:event may not be needed if getregionpos() provides the same info.