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>"
endif
let @/ = '\<'.l:word.'\>'
call histadd('search', '<' . l:word . '>')
let g:highlighting = 1
return ":silent set hlsearch\<CR>"
endfunction
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.