Skip to content

Instantly share code, notes, and snippets.

@qstrahl
Last active September 2, 2015 18:55
Show Gist options
  • Save qstrahl/638780bfba2262bb1d2b to your computer and use it in GitHub Desktop.
Save qstrahl/638780bfba2262bb1d2b to your computer and use it in GitHub Desktop.
safesearch.vim: start a search safely, without the risk of clobbering the last pattern
"" safesearch: start a search in vim without overwriting the last pattern
"" trivial? Yup. Useful? Ehhh maybe.
function! s:SafeSearch ()
augroup SafeSearch
autocmd CursorMoved,CursorMovedI,InsertEnter,CursorHold,CursorHoldI,CmdWinEnter,CmdWinLeave,WinEnter,WinLeave * call <SID>Teardown()
augroup END
let s:pattern = @/
return '/'
endfunction
function! s:Teardown()
call histdel('search', -1)
let @/ = s:pattern
augroup SafeSearch
autocmd!
augroup END
endfunction
noremap <expr> <Plug>(SafeSearch) <SID>SafeSearch()
map gj <Plug>(SafeSearch)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment