0

I'd like to keep the cursor at the middle of the screen wherever I am in the file, without scrolling. This mean to have blank space above the file when I'm at the start of the file, and have blank space under the file when I'm at the end of the file.

I now there is this answer who give a hack (only to have blank space after the file), but we have to remap all keys, which is not really convenient, and it does not work in insert or visual modes.

0

I have part of a solution.

I am not sure for the top of the file, but for the bottom of the file, if you use CTRL_e to scroll down, you can scroll as far as having the last line of the file at the top of your screen. You can also use CTRL_y to scroll up, but at the top of the file it will not scroll higher.

These two commands allow to scroll the view without moving your cursor (unlike using k and j to scroll up or down when the cursor is at the top or bottom). It is similar to scrolling with the mouse.

And it works in visual mode, however not in insert mode.

If anyone has suggestions for the uncovered cases (top of the file and insert mode, if it is possible), a completion of this answer would be welcome.

  • Thanks for your answer, but I'm looking for something automatic. I edited my post. – Phantom May 17 at 7:56
  • So when you move your cursor, you want the the view to automatically move so that it stays in the middle, correct? – padawin May 17 at 7:58
  • yes, that's it :) – Phantom May 17 at 8:10
  • Fair enough. From what I see online, it seems only possible through mappings, or configurations such as the link you provided or vim.fandom.com/wiki/… – padawin May 17 at 8:16
0

It's hard-coded in Vim that the first line can only be displayed at the top of the window, so in order to change this you will need a change in the source code.

However, the rest of your requirements (doesn't require remapping keys; works in visual and insert mode) can be met with autocommands that recentre the window whenever the cursor moves or when you change the contents of the buffer in insert mode:

augroup CentredCursor
  autocmd!
  autocmd CursorMoved * normal! zz
  autocmd TextChangedI * normal! zz
augroup END

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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