将搜索结果放在vi中的屏幕顶部

Put the search results at the top of the screen in vi

在每个 n 命令搜索下一个匹配项后,我希望 vi 自动将行定位在屏幕顶部。

zt 命令将重绘 vim 以便当前行位于屏幕顶部。来自 :h zt:

                            *z<CR>*
z<CR>           Redraw, line [count] at top of window (default
            cursor line).  Put cursor at first non-blank in the
            line.

                            *zt*
zt          Like "z<CR>", but leave the cursor in the same
            column.  {not in Vi}

因此,您可以在 .vimrc 中使用以下内容来完成此操作:

nnoremap n nzt
nnoremap N Nzt
xnoremap n nzt
xnoremap N Nzt

尽管就我个人而言,我更喜欢将它放在中间,所以我改为使用以下内容:

nnoremap n nzz
nnoremap N Nzz

使用:set so=999

使用这个,光标将永远在中间线。所以在每个 n 命令搜索下一个匹配项后 vim 自动将光标定位在屏幕的中间行。

这是有利的,因为我们可以看到搜索的上下文。

有关详细信息,请参阅 :help scrolloff