Ensure VisualNOS is only used under X11; fall back to Visual otherwise. by shaneharper · Pull Request #19659 · vim/vim · GitHub
Skip to content

Ensure VisualNOS is only used under X11; fall back to Visual otherwise.#19659

Closed
shaneharper wants to merge 1 commit intovim:masterfrom
shaneharper:do_not_use_VisualNOS_highlight_group_when_not_connected_to_an_X_server
Closed

Ensure VisualNOS is only used under X11; fall back to Visual otherwise.#19659
shaneharper wants to merge 1 commit intovim:masterfrom
shaneharper:do_not_use_VisualNOS_highlight_group_when_not_connected_to_an_X_server

Conversation

@shaneharper
Copy link
Copy Markdown
Contributor

@shaneharper shaneharper commented Mar 12, 2026

Problem: Vim could use the VisualNOS highlight group when not connected to an X server.
Solution: Always highlight visual selections with Visual when not connected to an X server.

When Vim is running with X11 support it chooses between the Visual and VisualNOS highlight groups for a visual selection based on whether it believes it owns the X selection. According to :help hl-VisualNOS, VisualNOS should only be used by the X11 GUI or when using the xterm-clipboard feature, however, prior to this commit Vim could choose VisualNOS when a clipboard provider was used, even when Vim was not connected to an X server.

I first noticed the issue after running:
:packadd osc52 | set clipmethod=osc52

I wrote the following test case. The test is effective if my shell's DISPLAY variable has been unset. I did not work out how to make the test work if the Vim instance running the test had connected to an X server (as then it may then use VisualNOS instead of Visual for highlighting the visual selection).

func Test_visual_highlight_when_using_a_clipboard_provider()
  " The test assumes different screen attributes will be used for Visual and for VisualNOS.
  CheckFeature clipboard_provider

  func s:get_attr_for_first_char_in_buf()
    let pos = screenpos(win_getid(), 1, 1)
    call assert_true(pos.row > 0 && pos.col > 0)
    redraw
    return screenattr(pos.row, pos.col)
  endfunc

  new +0put='X'
  call matchadd('Visual', '.')
  const Visual_attr = s:get_attr_for_first_char_in_buf()
  call clearmatches()

  normal! V$

  let v:clipproviders['custom'] = {}
  set clipmethod=custom
  " The following assert originally failed; VisualNOS, not Visual, was used.
  call assert_equal(Visual_attr, s:get_attr_for_first_char_in_buf())

  set clipmethod=
  call assert_equal(Visual_attr, s:get_attr_for_first_char_in_buf())

  unlet v:clipproviders['custom']
  set clipmethod&
  bwipe!
endfunc

@shaneharper shaneharper marked this pull request as draft March 12, 2026 23:05
@shaneharper shaneharper force-pushed the do_not_use_VisualNOS_highlight_group_when_not_connected_to_an_X_server branch from ef13337 to 8bac956 Compare March 12, 2026 23:27
@shaneharper shaneharper marked this pull request as ready for review March 13, 2026 00:04
@chrisbra
Copy link
Copy Markdown
Member

Problem: Vim could use the VisualNOS highlight group when not connected to an X server.
Solution: Always highlight visual selections with Visual when not connected to an X server.

When Vim is running with X11 support it chooses between the Visual and VisualNOS highlight groups for a visual selection based on whether it believes it owns the X selection. According to :help hl-VisualNOS, VisualNOS should only be used by the X11 GUI or when using the xterm-clipboard feature, however, prior to this commit Vim could choose VisualNOS when a clipboard provider was used, even when Vim was not connected to an X server.
@shaneharper shaneharper force-pushed the do_not_use_VisualNOS_highlight_group_when_not_connected_to_an_X_server branch from 8bac956 to e88d119 Compare March 13, 2026 08:19
@shaneharper
Copy link
Copy Markdown
Contributor Author

Can you add your test please and amend it like this:

Done.

Alternatively on could temporarily change $DISPLAY invalid at the beginning of the test and restoring it at the end but I am not sure if this would work as well (or if this is too late, because Vim has already setup X11 connection then).

I tried unlet $DISPLAY at the top of the test function but Vim will keep the X11 connection if it already had one.

@chrisbra
Copy link
Copy Markdown
Member

alright, thanks

@chrisbra chrisbra closed this in 8b009a3 Mar 13, 2026
@shaneharper shaneharper deleted the do_not_use_VisualNOS_highlight_group_when_not_connected_to_an_X_server branch March 20, 2026 21:40
@lilydjwg
Copy link
Copy Markdown
Contributor

lilydjwg commented Apr 4, 2026

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 this pull request may close these issues.

3 participants