Skip to content

Instantly share code, notes, and snippets.

@manasthakur
Last active January 13, 2018 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manasthakur/6652b547b1e0e7a869669d81a70cf2e5 to your computer and use it in GitHub Desktop.
Save manasthakur/6652b547b1e0e7a869669d81a70cf2e5 to your computer and use it in GitHub Desktop.
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