Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Search ignoring commented text
" Function to skip commented text while searching
function! SearchNoComments(pattern)
let l:matchpos = getpos('.')
let l:fileOver = []
while search(a:pattern, 'w') > 0
if l:fileOver == []
let l:fileOver = getpos('.')
elseif l:fileOver == getpos('.')
" Break if we are back to where we started
break
endif
if eval(synIDattr(synIDtrans(synID(line("."), col("."), 1)), "name") == "Comment")
" Continue if we are in a commented region
continue
endif
let l:matchpos = getpos('.')
break
endwhile
call setpos('.', l:matchpos)
endfunction
" Command to call the search function
command! -nargs=+ SearchNoComments :call SearchNoComments(<q-args>)
" Shorthand for the above command
nnoremap \\ :SearchNoComments<Space>
" Search the word under cursor
nnoremap \. :SearchNoComments <C-r><C-w><CR>
" Shorthand to continue searching forward (wrapping enabled above)
nnoremap <silent> \] :SearchNoComments <Up><CR>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment