Vim class search

How to define a vim function so that when called with

Foo

he searches through vimgrep for

\s*class Foo

or

\s*struct Foo

?

[This is poorman cscope / ctag; I want to be able to enter a class name and look for it for the class.]

If it's easy, is there a way I can tell it to look under my cursor to use this word as a name for a search?

Thank!

+3
source share
2 answers

Here's a hack from a vim newbie that seems to work:

function! SearchFunc()
  let l:filenames = substitute(glob("*.c") . glob("*.cpp") . glob("*.h"), '\n', ' ', 'g')
  try
    execute 'vimgrep /^\s*\(struct\|class\)\s*'  . expand("<cword>") . '/ ' . l:filenames
  catch
    echon 'No results found.'
    sleep 800m
  endtry
endfunction
nmap <Leader>fi :call SearchFunc()^M

This is necessary to search for a word under the cursor when entering \ fi.

Code Explanation:

vimgrep , , , try/catch/endtry. , , , , , .

"nmap" " ", - . , . Ctrl-V, Ctrl-M , ^ M, .

, , :

function! SearchFunc(findme)
  let l:filenames = substitute(glob("*.c") . glob("*.cpp") . glob("*.h"), '\n', ' ', 'g')
  try
    execute 'vimgrep /^\s*\(struct\|class\)\s*'  . a:findme . '/ ' . l:filenames
  catch
    echon 'No results found.'
    sleep 800m
  endtry
endfunction

,

:call SearchFunc('foo')

<cword> .

+2

<cword> :h expand().

fun! MySearch()
  exe 'vimgrep /\s*class '.expand('<cword>').'/ *'
endfun
+2

Source: https://habr.com/ru/post/1732814/


All Articles