vim restores cursor position; exclude special files

After reading through the manual on auto commands, I noticed that it seems not possible to define the pattern they match to in such a way that they exclude a special pattern. And I also wasn't able to use some variable that contains the current filename, so that I simply expand the existing if to exclude the file.

However, based on the comment by Pavel Shved (about the gg moving to the top of the file) I thought that in the same way it should be possible to simply overwrite the effect of the position restoring, by simply moving it to the top later again. So I came up with this:

autocmd BufReadPost COMMIT_EDITMSG
  \ exe "normal! gg"

Placing this after the previous autocmd BufReadPost simply chains the event execution, so vim, after executing the first and restoring the position, reads this and matches it on the excluded filename and uses the gg to move the cursor to the top, basically overwriting the effect of the original autocmd.

And that works just fine :)


I understand you have already come up with a solution but I had the same question and came up with an alternative that doesn't require chaining.

function! PositionCursorFromViminfo()
  if !(bufname("%") =~ '\(COMMIT_EDITMSG\)') && line("'\"") > 1 && line("'\"") <= line("$")
    exe "normal! g`\""
  endif
endfunction
:au BufReadPost * call PositionCursorFromViminfo()

Tags:

Vim