how to make n always moves to the middle

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

how to make n always moves to the middle

jose isaias cabrera-2

Greetings!

When I do a,

/search-string

I would then hit n for the next found string.  however, sometimes, the
next string is found at the last line and I would like to see the a few
lines below it.  And here is the question: Is there a way that I can
have the next n be in the middle, if it is X line or lower?  Or even
better, if it is X line or lower, set n to be at line Y.  Is this
possible?

thanks.

jic


Reply | Threaded
Open this post in threaded view
|

Re: how to make n always moves to the middle

Thomas Geulig
Am Friday 22 July 2005 17:58 schrieb jose isaias cabrera:

> When I do a,
>
> /search-string
>
> I would then hit n for the next found string.  however, sometimes, the
> next string is found at the last line and I would like to see the a few
> lines below it.  And here is the question: Is there a way that I can
> have the next n be in the middle, if it is X line or lower?  Or even
> better, if it is X line or lower, set n to be at line Y.  Is this
> possible?

Take a look at the 'scrolloff' option.

:h scrolloff

Thomas


Reply | Threaded
Open this post in threaded view
|

Re: how to make n always moves to the middle

A.J.Mechelynck
In reply to this post by jose isaias cabrera-2
----- Original Message -----
From: "jose isaias cabrera" <[hidden email]>
To: <[hidden email]>
Sent: Friday, July 22, 2005 5:58 PM
Subject: how to make n always moves to the middle


>
> Greetings!
>
> When I do a,
>
> /search-string
>
> I would then hit n for the next found string.  however, sometimes, the
> next string is found at the last line and I would like to see the a few
> lines below it.  And here is the question: Is there a way that I can
> have the next n be in the middle, if it is X line or lower?  Or even
> better, if it is X line or lower, set n to be at line Y.  Is this
> possible?
>
> thanks.
>
> jic

To scroll the match (found by n or N, not the first match found by / or ?)
to center of window:

    :map n nzz
    :map N Nzz

See also
    :help scroll-up
    :help scroll-down
    :help scroll-cursor

For instance, noremap to nzb5<C-Y> and Nzb5<C-Y> to move it to 5 lines above
the bottom.

HTH,
Tony.


Reply | Threaded
Open this post in threaded view
|

Re: how to make n always moves to the middle

jose isaias cabrera-2
In reply to this post by Thomas Geulig

Thanks.

On 18:12 Fri 22 Jul     , Thomas Geulig wrote:

> Am Friday 22 July 2005 17:58 schrieb jose isaias cabrera:
> > When I do a,
> >
> > /search-string
> >
> > I would then hit n for the next found string.  however, sometimes, the
> > next string is found at the last line and I would like to see the a few
> > lines below it.  And here is the question: Is there a way that I can
> > have the next n be in the middle, if it is X line or lower?  Or even
> > better, if it is X line or lower, set n to be at line Y.  Is this
> > possible?
>
> Take a look at the 'scrolloff' option.
>
> :h scrolloff
>
> Thomas
>
>

Reply | Threaded
Open this post in threaded view
|

Re: how to make n always moves to the middle

Hari Krishna Dara
In reply to this post by jose isaias cabrera-2

On Fri, 22 Jul 2005 at 6:12pm, Thomas Geulig wrote:

> Am Friday 22 July 2005 17:58 schrieb jose isaias cabrera:
> > When I do a,
> >
> > /search-string
> >
> > I would then hit n for the next found string.  however, sometimes, the
> > next string is found at the last line and I would like to see the a few
> > lines below it.  And here is the question: Is there a way that I can
> > have the next n be in the middle, if it is X line or lower?  Or even
> > better, if it is X line or lower, set n to be at line Y.  Is this
> > possible?
>
> Take a look at the 'scrolloff' option.
>
> :h scrolloff
>
> Thomas

The 'scrolloff' was too restrictive for me, as I wanted only search to
work this way, so here is what I do in my vimrc. This tailors only the
"n" and "N" commands, though in theory you can map other search commands
too (such as "*", "#" etc.). The comment below explains why I don't like
the simple solution proposed by Tony in another email:


" Remap some search mappings to position cursor always at the center after the
" hit.
" It is not nice (by my experience) to move each hit even if it is on the same
" page to the center, as it will lead to confusion on how far the hit was from
" the previous line.
nnoremap <silent> n :call SearchAndCenter('n')<CR>
nnoremap <silent> N :call SearchAndCenter('N')<CR>

function! SearchAndCenter(cmd)
  let v:errmsg = ""
  silent! exec "normal! " . a:cmd
  if v:errmsg != ""
    echohl ErrorMsg | echomsg v:errmsg | echohl None
    return
  endif

  if winline() == winheight(0) || winline() == 1
    normal! zz
  endif
endfunction

--
HTH,
Hari


               
____________________________________________________
Start your day with Yahoo! - make it your home page
http://www.yahoo.com/r/hs