haya14busa / incsearch.vim

:flashlight: Improved incremental searching for Vim
https://medium.com/@haya14busa/incsearch-vim-is-dead-long-live-incsearch-2b7070d55250
MIT License
1.11k stars 23 forks source link

Strange behavior when working with :terminal #155

Open tammersaleh opened 5 years ago

tammersaleh commented 5 years ago

I use :term in Vim 8.0, and every so often when switching to a terminal window, incsearch seems to send (_incsearch-nohlsearch) to the command line:

screenshot 2018-11-06 11 28 47

If I hit enter, then the text is sent to bash, so it's not a visual artifact. The relevant vimrc snippet is:

" Incsearch {{{
set hlsearch
let g:incsearch#auto_nohlsearch = 1
map /  <Plug>(incsearch-forward)
map ?  <Plug>(incsearch-backward)
map g/ <Plug>(incsearch-stay)
map n  <Plug>(incsearch-nohl-n)
map N  <Plug>(incsearch-nohl-N)
map *  <Plug>(incsearch-nohl-*)
" map #  <Plug>(incsearch-nohl-#) " I use tcomment
map g* <Plug>(incsearch-nohl-g*)
map g# <Plug>(incsearch-nohl-g#)
let g:incsearch#separate_highlight = 1
" }}}
mars90226 commented 5 years ago

I've found a solution. Add the following snippet to the .vimrc.

augroup incsearch_settings 
  autocmd!
  autocmd BufWinLeave,WinLeave * call s:clear_incsearch_nohlsearch()
augroup END

function! s:clear_incsearch_nohlsearch()
  nohlsearch
  silent! autocmd! incsearch-auto-nohlsearch
endfunction

Because g:incsearch#auto_nohlsearch = 1, incsearch.vim will create autocmd after search that will execute :nohlsearch on CursorMoved. But in terminal mode, that command will be directly send to your shell without executing. The above snippet will clear the autocmd on BufWinLeave and WinLeave which will happen when you leave current window or leave current buffer.