In vim, how do you scroll a buffer so the cursor location is centered in the screen?

后端 未结 5 432
半阙折子戏
半阙折子戏 2021-02-02 06:42

In vim, often I will jump to a mark I made, or a search result, and the cursor will be at the very bottom or very top of the screen. At this point, in order for the screen to b

相关标签:
5条回答
  • 2021-02-02 06:49

    There is a way to keep the cursor centered even near EOF.

    scrolloff=999 works fine except near the end of the buffer where it does not center the cursor, I'm not aware of any fix that allows scrolloff to keep the cursor centered at the end of the buffer.

    An alternative to scrolloff=999 is to remap your navigation commands to center on cursor. I do the following in my _vimrc/.vimrc:

    " Avoids updating the screen before commands are completed
    set lazyredraw
    
    " Remap navigation commands to center view on cursor using zz
    nnoremap <C-U> 11kzz
    nnoremap <C-D> 11jzz
    nnoremap j jzz
    nnoremap k kzz
    nnoremap # #zz
    nnoremap * *zz
    nnoremap n nzz
    nnoremap N Nzz
    

    This will keep the cursor centered vertically all the way to the end of the buffer :)

    0 讨论(0)
  • 2021-02-02 06:49

    The scrollfix.vim plugin is great for this. That is what I use, and it works like a charm. You can find it on github here.

    The scrollfix plugin allows you to control exactly where (vertically along the buffer) your cursor stays fixed. By default it is at 60% of the buffer from the top, but this is customizable. This affects the position of the cursor in the normal as well as in the insert mode.

    0 讨论(0)
  • 2021-02-02 06:51

    This will center the current line

    zz
    

    Optionally you could set scrolloff to something large like 999 and the working line will always be in the center, except when you are towards the start or end of the file.

    :set scrolloff=999
    
    0 讨论(0)
  • 2021-02-02 06:57

    The 'scrolloff' (scroll offset) option determines the number of context lines you would like to see above and below the cursor. Setting it to, say, 5 makes it so there are always 5 lines visible above and below the cursor while moving/scrolling. Setting 'scrolloff' to a large value causes the cursor to stay in the middle line when possible:

    :set so=999
    

    To restore normal behavior, enter:

    :set so=0
    

    If you're switching between those a lot, you can create a mapping to toggle quickly:

    :nnoremap <Leader>ts :let &scrolloff=999-&scrolloff<CR> " ToggleScrolloff
    
    0 讨论(0)
  • 2021-02-02 07:16

    You have to press z twice, like: zz

    0 讨论(0)
提交回复
热议问题