VIM: Avoid a search result is shown at the first or last line of the screen?

50 Views Asked by At

If I search forward or backwards (via / and ?) in VIM it will show results at the last line of the screen (when searched forward) or first line of the screen (when searched backwards).

The problem is that in this case I can't see the lines before or after the search result without moving the cursor up/down.

How can I force VIM to scroll the search result to the middle of the screen or alternatively scroll it in such way that always X lines before and after the search result are visible in the terminal screen?

Thanks

1

There are 1 best solutions below

0
Manjunath Mohan On
  1. set scrolloff=10 will force vim to keep 10 min lines above and below the cursorline

  2. You can force vim to keep cursor in the middle of the screen when navigating to the next & prev search results by remapping n and N keys

nnoremap n nzz
nnoremap N Nzz

zz is a keymap to move the cursor line to middle of the screen