I'm trying to streamline my workflow. I want one keystroke to

  • slurp the token that my cursor is on, setting it in the search register
  • enable hlsearch to show where else it is in the buffer
  • not move the cursor
  • report the number of occurrences of this search pattern in the buffer

When hitting the same key again,

  • if on the same token as already in search register, turn off hlsearch
  • otherwise behaves the same as above

Previously I used a bit of VimL I wrote to implement some of this:

let g:highlighting = 1
function! SearchForWord()
    let l:word = expand('<cword>')
    if g:highlighting == 1 && @/ =~ '^\\<'.l:word.'\\>$'
        let g:highlighting = 0
        return ":silent nohlsearch\<CR>"
    let @/ = '\<'.l:word.'\>'
    call histadd('search', '<' . l:word . '>')
    let g:highlighting = 1
    return ":silent set hlsearch\<CR>"

nnoremap <silent> <expr> <CR> &buftype == "" ? SearchForWord() : "<cr>"

But I am deep in a new round of overhauling and refactoring Vim config in lua. I'm not opposed to keeping around vimscript sections if necessary. But as part of this effort I'm looking to make the experience even better than before.

I am now tentatively using https://github.com/inkarkat/vim-SearchHighlighting and it does satisfy my requirements, and adds in the report of the count of search matches, which I'm really trying to add now.

The author has thought of everything! According to the plugin docs, %s///gn<CR> can be tacked onto the bind to enable this report. But running any :s command will move the cursor to the beginning of the line. How can I make this not move the cursor?

  • :%s/<C-r><C-w>/gn is good enough for me.
    – romainl
    Commented Mar 14, 2023 at 15:11

2 Answers 2



The above is precisely what I wanted, nothing more and nothing less. So I changed the bind back to vim.keymap.set('n', '<CR>', '<Plug>SearchHighlightingStar') and these plugins give desired behavior out of the box.


A simple fix is to use marks (see :help mark-motions) to save/restore cursor location,

  1. Type mm (in normal mode) to save your current cursor location (to the mark m)
  2. Perform the :%s///gn<CR> to do the counting job.
  3. Type `m to restore your cursor location.
  • You are correct, I did comment on Ingo's SearchHighlighting repo and he gave a solution using marks as well. I'll probably use them for this purpose at some point even though i've found workarounds without them for now
    – Steven Lu
    Commented Mar 19, 2023 at 0:52
  • @StevenLu Using marks for manual keystrokes or mappings is fine as it's quick and simple. Still I think it may not be a good idea for serious vimscripts because there are better solutions like getcurpos, and you either have to save/restore mark positions (which is cumbersome) or clobber certain marks (which the user may want to use).
    – Hoblovski
    Commented Mar 20, 2023 at 6:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.