Le lundi 25 novembre 2013 11:51:51 UTC+1, Paolo Bolzoni a écrit :
> Dear list,
> 
> I often search text in the latex documents to move around.
> So I press /<some known sentence> and Vim shows me the
> place because I have incsearch active.
> 
> Can I just stay in the place without pressing enter?
> I am asking because I also have hlsearch on and while
> it is normally useful in this particular case is not.
Based on this very real annoyance, and having a little bit of time to kill, I
wrote the code below: not only does it removes the 'hlsearch' problem, but it
also makes search easier by foregrounding matches and letting you jump from
one to the next without leaving the search. I find it quite comfortable, but
of course others might find that just terrible. Here's how it works:
- Type <C-f> (in normal mode).
- Type your regex search as you would with a normal search (it is echoed in
the command line).
- Use <Tab> to jump to the next match, and <S-Tab> to jump to the previous
one. You can also continue searching after that.
- Hit <Enter> to exit and position the cursor on the current match.
- Hit <Esc> to end the whole thing without moving the cursor.
- You might want to change the color in the line:
    hi searchRest guifg=#AAAAAA
or adapt it for the terminal (I'm using gVim).
If anybody is interested, I can make that into a plugin (rewriting it a little
bit less sloppily, because it will fail in some cases, plus it requires a few
options to be really interesting), in case, of course, such a plugin does not
already exist :)
Best,
Paul
" Put this code somewhere.
hi link searchFound Search
hi link searchHigh  Error
hi searchRest guifg=#AAAAAA
function! s:search (str, opos, ...)
  let str = a:str
  echohl Type
  redraw
  echo "Searching for: "
  echohl None
  echon str
  let ch = getchar()
  for n in a:000
    if n >= 0
      call matchdelete(n)
    endif
  endfor
  " <Esc>: cancel the whole thing.
  if ch == 27
    call cursor(a:opos[1], a:opos[2])
  " Other chars, except <Enter>, which does nothing (the cursor is already
  " where it must be).
  elseif ch != 13
    if ch == "\<BackSpace>"
      let str = substitute(str, '.$', '', 'g')
    elseif ch != 9 " 9 is <Tab>.
      let str  .= nr2char(ch)
    end
    let rest  = matchadd('searchRest', '.*')
    let found = matchadd('searchFound', str)
    " <Tab> moves to the next match, <S-Tab> to the previous one.
    let f = ch == 9 ? '' : (ch == "\<S-Tab>" ? 'b' : 'c')
    let pos   = searchpos(str, f)
    call cursor(pos)
    let high  = matchadd('searchHigh', '\%' . pos[0] . 'l\%' . pos[1] .'c' . str)
    call s:search(str, a:opos, rest, found, high)
  endif
endfunction
nnoremap <C-f> :call <SID>search('', getpos('.'))<CR>
" End of code.
-- 
-- 
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
--- 
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment