Re: how to map the n to nzz just in the jump in search result ?

159 views
Skip to first unread message

Tim Chase

unread,
Jul 16, 2016, 7:52:33 AM7/16/16
to 李哲, vim_use
On 2016-07-15 21:30, 李哲 wrote:
> as you know when you search `abc` in the text, you press `n` to go
> to the next match,
>
> now I want to map n here to nzz, what should I do?
>
> nmap n nzz obvious wrong here.

By using "nmap" rather than "nnoremap", you end up with a recursive
macro that does an "n" that does an "n" that does an "n", to infinity.

Should be able to just change it to

:nnoremap n nzz

and you're good to go.

-tim


Arun

unread,
Jul 17, 2016, 2:56:53 AM7/17/16
to vim...@googlegroups.com, 李哲
As it appears that the intent here is to re-center the searched line, another approach is to set a high 'scrolloff' lines, like

  :set scrolloff=999

That way, even backward search (or any other jump for that matter) would also re-center the screen.

--Arun 

Tim Chase

unread,
Jul 17, 2016, 8:03:00 AM7/17/16
to vim...@googlegroups.com
On 2016-07-16 23:56, Arun wrote:
> On Sat, Jul 16, 2016 at 4:52 AM, Tim Chase <v...@tim.thechases.com>
> > :nnoremap n nzz
>
> As it appears that the intent here is to re-center the searched
> line, another approach is to set a high 'scrolloff' lines, like
>
> :set scrolloff=999
>
> That way, even backward search (or any other jump for that matter)
> would also re-center the screen.

Yeah, I wondered that, but it's one of those things that I find is
nice in certain instances, but drives me nuts if it's on all the
time. Other such things are 'hls' and having all yanks/deletions go
to my system clipboard. I find that being intentional in the couple
cases where I do want such features annoys me far less than having
them on all the time.

But if the OP wants the cursor centered all the time, that's
definitely the way to go.

-tim



Bee

unread,
Jul 17, 2016, 9:54:30 AM7/17/16
to vim_use
On Sunday, July 17, 2016 at 5:03:00 AM UTC-7, Tim Chase wrote:
> On 2016-07-16 23:56, Arun wrote:
> > On Sat, Jul 16, 2016 at 4:52 AM, Tim Chase
> > > :nnoremap n nzz
> >
> > As it appears that the intent here is to re-center the searched
> > line, another approach is to set a high 'scrolloff' lines, like
> >
> > :set scrolloff=999
> >
> > That way, even backward search (or any other jump for that matter)
> > would also re-center the screen.
>
> Yeah, I wondered that, but it's one of those things that I find is
> nice in certain instances, but drives me nuts if it's on all the
> time. Other such things are 'hls' and having all yanks/deletions go
> to my system clipboard. I find that being intentional in the couple
> cases where I do want such features annoys me far less than having
> them on all the time.
>
> But if the OP wants the cursor centered all the time, that's
> definitely the way to go.
>
> -tim

No need to have it on always:

function! Scoff()
let &scrolloff = &scrolloff==1 ? 555 : 1
endfun
nmap <special> <silent> <F6> :call Scoff()<cr>
imap <special> <silent> <F6> <c-o>:call Scoff()<cr>

Bill

Nikolay Aleksandrovich Pavlov

unread,
Jul 17, 2016, 10:28:48 AM7/17/16
to vim...@googlegroups.com
You must not use *map without nore unless you have a clear explanation
why you prefer them. I.e. nnoremap and inoremap should be the default
always.

This best practice avoids issues like OP has. Your mappings are a
source of the problem if e.g. a user has not too uncommon `noremap :
;`+`noremap ; :` mappings that swap : and ;.

>
> Bill
>
> --
> --
> 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+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Bee

unread,
Jul 17, 2016, 11:55:09 AM7/17/16
to vim_use

Do I understand correctly?
Always use nnoremap or inoremap
Unless recursion is desired

Nikolay Aleksandrovich Pavlov

unread,
Jul 17, 2016, 1:03:39 PM7/17/16
to vim...@googlegroups.com
Yes. I additionally suggest that “recursion is desired” should be read
as “every single key in {rhs} is being remapped” if you are writing a
plugin: this leaves only mappings like `nmap default
<Plug>(MyFooPluginMapping)` which is somewhere preceded by something
like `nnoremap <Plug>(MyFooPluginMapping) :<C-u>call
myfooplugin#function()<CR>`, so the whole {rhs} is being remapped to
something else. But this is too strict if you are writing something
for your vimrc.

Tim Chase

unread,
Jul 17, 2016, 1:05:30 PM7/17/16
to vim...@googlegroups.com
On 2016-07-17 08:55, Bee wrote:
> Do I understand correctly?
> Always use nnoremap or inoremap
> Unless recursion is desired

That's a very succinct & correct summary. :-)

-tim



Reply all
Reply to author
Forward
0 new messages