我很高兴在我的 .vimrc 中设置scrolloff=999
- 这使光标始终位于屏幕中央,让我只需考虑移动光标并重新调整所有垂直滚动键绑定的用途。
我希望水平滚动也能自动工作(当设置nowrap
时),并且最近发现了sidescrolloff
,但将其设置为高值不会带来良好的体验 - 编辑行尾时,行首经常从屏幕上消失。
有没有办法让 vim 将光标保持在屏幕右边缘至少 n
个字符,同时尽可能急切地将滚动返回到最左侧?
示意性地,
horizontalScrollPos = max(cursorPos + n - screenWidth, 0)
似乎没有 vim 附带的选项可以让你做你想做的事。 但是,它可以通过一些vim魔法(或黑客)来完成。
" Only scroll once to the side when you hit the side of the screen.
" (or the padding specified in sidescrolloff)
set sidescroll=1
" This sets the padding on the right side of the screen.
set sidescrolloff=4
" Make a script-local function for use in a later autocommand. It
" is not made global because nothing else should need to use it.
function s:FixScroll()
" Check if wrap is set, and if it is, return from the function.
" This is so that the entire function isn't run if wrap is on.
if &wrap
return 0
" This if statement checks if the horizontal scroll amount is
" 0, and returns from the function if it is. This way
" the rest of the function isn't run when unnecessary.
elseif col(".") - wincol() - &fdc + &number * &numberwidth ==# 0
return 0
endif
" Figure out which column that the cursor needs to be in when
" scrolling. 'sidescrolloff` is the option set earlier, or
" the padding from the right side of the screen.
let l:targetcol = winwidth(0) - &sidescrolloff
" This checks to see if the cursor is to the left of the target
" column.
if wincol() < l:targetcol
" Figure out how far to scroll to the left to get the
" cursor in the target column.
let l:move_amount = l:targetcol - wincol()
" Use 'zh' to scroll to the left by the specified amount
" using the exe command.
exe 'normal! ' . l:move_amount . 'zh'
endif
endfunction
" Run the local fixscroll function every time the cursor is moved in
" normal or insert mode.
autocmd CursorMoved,CursorMovedI * call <SID>FixScroll()
相关帮助主题:
:help 'sidescrolloff'
:help 'sidescroll'
:help col()
:help wincol()
:help winwidth()
:help zh
:help zl
:help autocmd-groups-abc