I have the following autocmd in my vimrc.

augroup line_return
  au BufReadPost *
    \ if line("'\"") > 0 && line("'\"") <= line("$") |
    \     execute 'normal! g`"zvzz' |
    \ endif
augroup END

It moves the cursor to the line it was on when the file was previously opened.

I like this behaviour generally, but I don't want this behaviour when writing a Git commit. I tried adding the following autocmd, but it doesn't seem to work. I don't think the syntax is wrong since I see no errors when starting Vim. I think it's because it runs before the first autocmd runs, but I'm not sure.

augroup always_start_at_top_of_git_commit
  au FileType gitcommit execute 'normal! gg'
augroup END

How can I make this work? Should I actually just add a conditional to the first autocmd instead?


Have you checked :h last-position-jump with a recent vim?

It has almost the same autocommand as yours but with 'commit' in mind:

            *restore-cursor* *last-position-jump*  >
autocmd BufReadPost *
  \ if line("'\"") >= 1 && line("'\"") <= line("$") && &ft !~# 'commit'
  \ |   exe "normal! g`\""
  \ | endif

See the last condition &ft !~# 'commit' -- it checks the filetype of the buffer and if it has no 'commit' in it then jump to the given position.

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.