JustJeff
JustJeff

Reputation: 12980

Automatically center vim search results

When I do a search with vim or gvim, the resulting positioning of the cursor within the window is somewhat random, all too frequently landing on the last (or first) line in the window. Search highlighting helps, but it's still cumbersome to have to look all around on the screen to find the cursor ... and a bit ironic, that after vim locates the next result in some megabytes-long log file, I have to use the ol' neocortex to isolate it from the last 4K or so.

I can manually get the effect I want by hitting 'zz' after every search, but would prefer to doctor up my _vimrc to make this happen automatically.

Upvotes: 5

Views: 2639

Answers (4)

steffen
steffen

Reputation: 16938

I had been using the nzz mapping for some time until I discovered an option that works better for me. I was also annoyed by results being displayed on the first or last line on screen.

This option keeps the cursor (and therefore the current search result) 5 lines below/above the first/last line.

:set scrolloff=5

I prefer this because nzz is annoying in case you have many search results on one page and each n scrolls the buffer.

Upvotes: 2

Maltimore
Maltimore

Reputation: 504

As @Mike-B pointed out, the accepted answer of doing

nmap n nzz

does not open folds. As an alternative I propose the following. It is a bit hacky, but seems to work for me:

nmap nzvzz

and equivalent for the other motions, if you want. zv should open all folds till the current cursor position is visible.

Upvotes: 1

Mosh
Mosh

Reputation: 2628

I use this trick with other commands too:

nnoremap n nzz
nnoremap N Nzz
nnoremap <C-o> <C-o>zz
nnoremap <C-i> <C-i>zz

Upvotes: 2

Rook
Rook

Reputation: 62528

Will this work for you ?

:nmap n nzz  
:nmap p pzz

Upvotes: 11

Related Questions