3

I recently started using Vim to write my notes after moving away from VS Codium for this. I'm really enjoying the speed and the ability to navigate keyboard only, but there is one thing that is slightly annoying and I was wondering if there is already a solution.

I understand that scrolloff will keep space above and below a cursor but this is when scrolling through a file. It doesn't work when typing. I understand that zz will recenter the file on the cursor position and that Ctrl-e moves down one line. So when I'm writing and I'm reaching the end of the file, I could zz or Ctrl-e to adjust the position. However, I was wondering if there was a way to automate this, something like:

if (eof - :.) < 5
  zz

Or something along those lines?

1
  • It sort of works, but not quite how I was hoping. I think it's the closest I'll get anyway based on what I've been finding. Maybe it's time to get a bit of experience with vimscript to see if I could do something like that. Thank you for the reply though, it's closer than what I've found!
    – pSYoniK
    Oct 26, 2022 at 18:44

1 Answer 1

2

You use autocommands to invoke <C-E> when the cursor gets within &scrolloff of the bottom of the window.

Here's an adaptation of my answer to a similar question that implements the above:

augroup ScrollOffAtEnd
  autocmd!
  autocmd CursorMoved * call ScrollOffAtEnd(0)
  autocmd TextChangedI * call ScrollOffAtEnd(1)
augroup END

function ScrollOffAtEnd(insert_mode) abort
  while winheight(0) > &scrolloff && winline() > winheight(0) - &scrolloff
    let at_end = a:insert_mode ? getcursorcharpos()[2] > len(getline('.')) : 0
    execute "normal!\<C-E>"

    " Fix position of cursor when 'wrap' occurs
    if at_end
      let cursor_pos = getcursorcharpos()
      let cursor_pos[2] = cursor_pos[2] + 1
      call setcursorcharpos(cursor_pos[1:])
    endif
  endwhile
endfunction
2
  • This is more like what I was trying to describe. Right now this does what I wanted, which is to keep adding space below the line I'm currently typing. So that's great, thank you!
    – pSYoniK
    Oct 28, 2022 at 12:04
  • @pSYoniK Glad I could help.
    – Rich
    Oct 28, 2022 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.