Skip to content

auto nohlsearch #15039

@habamax

Description

@habamax

'hlsearch' is very useful but at the same time too intrusive.

Help topic states specifically that :nohlsearch doesn't work in autocommand (actually it also doesn't work in timer callbacks) so there is no obvious way to make it auto turned off.

There is vim-cool plugin by @romainl and the essense of it might be the following snippet I am currently using:

# turn off hlsearch after:
# - doing nothing for 'updatetime'
# - getting into insert mode
augroup auto_nohlsearch | au!
    set updatetime=2000
    noremap <Plug>(nohlsearch) <cmd>nohlsearch<cr>
    noremap! <expr> <Plug>(nohlsearch) execute('nohlsearch')[-1]
    au CursorHold * call feedkeys("\<Plug>(nohlsearch)", 'm')
    au InsertEnter * call feedkeys("\<Plug>(nohlsearch)", 'm')
augroup END

Should we add this example, even though it is "hackish", to the help topic right after :h :nohlsearch or maybe even a simple plugin in the same way comment was added?

asciicast

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions