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

Highlighting toggles on when sourcing vimrc containing set hlsearch #43

Open
craigmac opened this issue Mar 23, 2022 · 7 comments
Open

Comments

@craigmac
Copy link

When I have set hlsearch set in my vimrc file and I source it, vim-cool highlights the last search. From my quick look, it seems to be triggered by the OptionSet autocmd. If I commented out set hlsearch in my vimrc and source it, no highlighting appears. Is there a way to have set hlsearch in your vimrc without having vim-cool re-highlight last search when I source the file?

@romainl
Copy link
Owner

romainl commented Mar 23, 2022

Hmm… what is the output of :echo &viminfo?

@craigmac
Copy link
Author

'100,<50,s10,h

@romainl
Copy link
Owner

romainl commented Mar 23, 2022

Yeah, the reasoning behind this is that set hlsearch is an explicit attempt to activate search highlighting, and thus that the expected outcome is to have the last search matches highlighted. In short: one enables "feature that highlights something" -> one gets highlighting. A bit like syntax on: you enable syntax highlighting, you get syntax highlighting.

Now, two questions:

  • is it really explicit if it's done as part of :source $MYVIMRC?
  • isn't that a special case that requires special handling?

@craigmac
Copy link
Author

Could there be a workaround to check if the OptionSet was fired as result of last command being the sourcing of a Vim filetype buffer?

@romainl
Copy link
Owner

romainl commented Mar 23, 2022

OptionSet has a rich API but it doesn't have that information. We can probably parse the output of :verbose set hlsearch?, but how to discriminate between the vimrc being sourced normally, as part of the startup process, and the vimrc being sourced manually? Maybe by inspecting the history?

@craigmac
Copy link
Author

Yeah, I'm not sure there's a foolproof way to do it. For now, I'm just going to stick a call my :nohlsearch binding manually after I save my vimrc: autocmd BufWritePost $MYVIMRC nested source $MYVIMRC | call feedkeys("\<C-l>")

craigmac added a commit to craigmac/vimfiles that referenced this issue Mar 25, 2022
@acc987
Copy link

acc987 commented Aug 24, 2023

I don't use vim-cool, but even with a :noh mapping one will also suffer from the issue you described and I worked around it by using a dedicated mapping for sourcing $MYVIMRC:

nnoremap <silent> ,s :source $MYVIMRC<C-R>=v:hlsearch ? '' : ' <Bar> nohlsearch'<CR><CR>

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

No branches or pull requests

3 participants