1

I use * and # for quick and basic typo checking.

If I type AVeryLongVariableName, I can put my cursor on the word, press * and see all the other places that word is used. I know I spelled it correctly. If nothing else is highlighted and the cursor does not move anywhere, I know I misspelled it.

I would like to do that for multiple words. For example, when I have identifiers foo and bar all over the place, I might want to look for foo.bar and not match foo or bar by themselves.

I use /foo.bar for this. Is there a more elegant way to maybe select foo.bar and show me all the places that string is matched, preferably a way I don't have to type the string?

3
  • BTW, maybe you simply want fuzzy search? look for the plugin leaderF. It supports preview of all options and regex search (exact) – eyal karni Dec 6 '20 at 13:31
  • leaderF looks cool but if I'm looking to make sure I spelled an expression correctly, I want the opposite of fuzzy. I need character-for-character precision. – John Schmitt Dec 6 '20 at 16:01
  • LeaderF in regex mode does this. Regex is precise. – eyal karni Dec 7 '20 at 0:01
1

Well, I have a code just for this.It uses the quickfix window to display all instances of a string or a search. :MC string, or select an instance of string and press Y, or M<motion>.

function! SpecialFind(type)
  let &selection = "inclusive"
    exec 'normal! `[v`]"xy'
    call Matches(@x)
endfunction

nnoremap M :set opfunc=SpecialFind<CR>g@

vnoremap Y "xy:call Matches(@x)<CR>

command! -nargs=1 MESC call Matches(<f-args>)
command! -nargs=1 MC call MatchesF(<f-args>)
command! -nargs=1 MW call MatchesF('\<'.<f-args>.'\>')
command! -nargs=1 M call MatchesF('\c'.<f-args>)

function! Matches(pat)
    let buffer=bufnr("") "current buffer number
    let b:lines=[]
    "the right way to escape!!
    execute ":%g/\\V" . escape(a:pat,'/\?') . "/let b:lines+=[{'bufnr':" . 'buffer' . ", 'lnum':" . "line('.')" . ", 'text': escape(getline('.'),'\"')}]"
    call setqflist(b:lines)
    copen
endfunction
function! MatchesF(pat)
    let buffer=bufnr("") "current buffer number
    let b:lines=[]
    "no escape!!
    execute ":%g/" . a:pat . "/let b:lines+=[{'bufnr':" . 'buffer' . ", 'lnum':" . "line('.')" . ", 'text': escape(getline('.'),'\"')}]"
    call setqflist(b:lines)
    copen
endfunction

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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