Skip to content

Instantly share code, notes, and snippets.

Last active January 13, 2018 14:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
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
if eval(synIDattr(synIDtrans(synID(line("."), col("."), 1)), "name") == "Comment")
" Continue if we are in a commented region
let l:matchpos = getpos('.')
call setpos('.', l:matchpos)
" 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