If I edit a file with vim that I have previously edited, I'd like vim to jump to the position I was in when I last closed the file.
How can I accomplish this?
What I use (adapted from
" Go to the last cursor location when a file is opened, unless this is a " git commit (in which case it's annoying) au BufReadPost * \ if line("'\"") > 0 && line("'\"") <= line("$") && &filetype != "gitcommit" | \ execute("normal `\"") | \ endif
BufReadPost gets run when starting to edit a bufffer. The
" mark is the cursor position when last exited the file (as remembered by viminfo). We first check if this is set higher than 0, and lower than the number of lines in the file, before jumping to it.
We don't do this if the filetype is gitcommit, since I find this annoying (I always want it to start at the first line).
You need to keep
viminfo file for all of this, which is enabled by default.
The vim-lastplace plugin does exactly what you want (I am the author). It improves on the above code snippet by handling commit messages intelligently. If you have debian-testing you can apt-get install vim-lastplace to quickly try it out.