Skip to content

Instantly share code, notes, and snippets.

@drasill
Last active May 25, 2016 09:48
Show Gist options
  • Save drasill/110a792d8fec6ebae9c071c833f8f1be to your computer and use it in GitHub Desktop.
Save drasill/110a792d8fec6ebae9c071c833f8f1be to your computer and use it in GitHub Desktop.
vim: search only inside/outside syntax type
" http://stackoverflow.com/questions/2683521/vim-search-in-c-c-code-lines
function! SearchWithSkip(pattern, flags, stopline, timeout, skip)
"
" Returns true if a match is found for {pattern}, but ignores matches
" where {skip} evaluates to false. This allows you to do nifty things
" like, say, only matching outside comments, only on odd-numbered lines,
" or whatever else you like.
"
" Mimics the built-in search() function, but adds a {skip} expression
" like that available in searchpair() and searchpairpos().
" (See the Vim help on search() for details of the other parameters.)
"
" Note the current position, so that if there are no unskipped
" matches, the cursor can be restored to this location.
"
let l:matchpos = getpos('.')
" Loop as long as {pattern} continues to be found.
"
while search(a:pattern, a:flags, a:stopline, a:timeout) > 0
" If {skip} is true, ignore this match and continue searching.
"
if eval(a:skip)
continue
endif
" If we get here, {pattern} was found and {skip} is false,
" so this is a match we don't want to ignore. Update the
" match position and stop searching.
"
let l:matchpos = getpos('.')
break
endwhile
" Jump to the position of the unskipped match, or to the original
" position if there wasn't one.
"
call setpos('.', l:matchpos)
endfunction
function! SearchOutside(synName, pattern)
"
" Searches for the specified pattern, but skips matches that
" exist within the specified syntax region.
"
call SearchWithSkip(a:pattern, '', '', '',
\ 'synIDattr(synID(line("."), col("."), 0), "name") =~? "' . a:synName . '"' )
endfunction
function! SearchInside(synName, pattern)
"
" Searches for the specified pattern, but skips matches that don't
" exist within the specified syntax region.
"
call SearchWithSkip(a:pattern, '', '', '',
\ 'synIDattr(synID(line("."), col("."), 0), "name") !~? "' . a:synName . '"' )
endfunction
command! -nargs=+ -complete=command SearchOutside call SearchOutside(<f-args>)
command! -nargs=+ -complete=command SearchInside call SearchInside(<f-args>)
@drasill
Copy link
Author

drasill commented May 25, 2016

To use it with vim-plug :

Plug 'https://gist.github.com/drasill/110a792d8fec6ebae9c071c833f8f1be',
    \ { 'dir': g:plug_home.'/vim-search-in-syntax/plugin', 'rtp': '..' }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment