Search code examples
unixsolarisvi

vi set default found line to center of screen?


Using vi on a Solaris 10 system. When I search for a string with the common syntax

/foo<CR>

the placement of the found line is semingly random within the 60 lines on my display. Sometimes it is near the top, sometimes middle, often the last line.

I would like it to be at a consistent location, (somewhere above the bottom). The following will always place it in the middle

/foo/z.<CR>

How can I make that the default behavior so that if I simply type

/foo<CR>

it will display the found line in the middle every time?


Solution

  • Adding in your ~/.vimrc something like:

    let g:in_search = 0
    function! StartSearch()
      let g:in_search = 1
    endfunction
    
    function! EndSearch()
      if g:in_search
        normal! zz
      endif
      let g:in_search = 0
    endfunction
    
    nnoremap / <ESC>:call StartSearch()<CR>/
    cnoremap <CR> <CR>:call EndSearch()<CR>
    nnoremap n nzz
    nnoremap N Nzz
    

    could do the trick.

    But maybe you just need:

    set incsearch
    set hlsearch
    

    ?!