0

Sometimes on start, the cursor is not at the top in gitcommit buffers. I assume that this is because it wasn't on the first line the last time I exited a gitcommit buffer and the position was saved in .viminfo.

How do I make the cursor appear at the top of gitcommit buffers?

As this is filetype specific, I'd like to put the solution in my gitcommit ftplugin file rather than in my general vimrc or autocmds.

3
  • "How do I make the cursor appear at the top of gitcommit buffers?" Remove from your config whatever causes the unwanted behavior.
    – romainl
    Commented May 31, 2023 at 15:23
  • @romainl I didn't say every buffer
    – paradroid
    Commented May 31, 2023 at 15:26
  • 1
    I didn't either. Anyway, the behavior you describe is not the expected behavior so you have to investigate your config. We can help you find the cause if you post your config.
    – romainl
    Commented May 31, 2023 at 15:28

1 Answer 1

1

Even though Vim saves the last known cursor position into viminfo file upon exit, it doesn't restore it by default. Instead, there is a piece of VimScript code at :h restore-cursor (also in defaults.vim) that may be used to perform this task. So practically everyone re-uses it.

If you look into the code you'll see that it tries to avoid restoring cursor position in git commit files specifically. So you need to inspect your custom config and find out what is the modified version you have there and why it doesn't work 100% correctly.

An alternative approach, that I'm using in my own config, is to capture VimLeavePre event and to wipe all offending buffers there, so they couldn't get into viminfo at all. However, one still needs to define all logical checks - by file type, by contents, by other flags, etc., even in this case.

And to make it clear, you can't do this by ftplugin, as restoring the cursor position normally works by BufReadPost event, not by FileType event.

2
  • Yes, I remember that it was not always the case, but vim is opening files with the cursor in the previous position, but nvim isn't (they share nearly all the same configuration). I guess I will have to find what is doing this by commenting out half the configuration pages at a time, as there are several of them.
    – paradroid
    Commented May 31, 2023 at 18:49
  • gvim is not keeping the cursor position persistent, which is strange, as it uses a superset of the same configuration. I don't mind this situation, but not for gitcommit.
    – paradroid
    Commented May 31, 2023 at 19:00

Your Answer

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

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