Last active
January 13, 2018 14:03
-
-
Save manasthakur/6652b547b1e0e7a869669d81a70cf2e5 to your computer and use it in GitHub Desktop.
Search ignoring commented text
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" 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