In vim, how do I get a file to open at the same line number I closed it at last time?

From Ubuntu's /etc/vim/vimrc file, this example is commented out:

" Uncomment the following to have Vim jump to the last position when                                                       
" reopening a file
if has("autocmd")
  au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")
    \| exe "normal! g'\"" | endif
endif

If this doesn't work, a common problem is not having ownership of your ~/.viminfo file. If this is the case, then run:

sudo chown user:group ~/.viminfo

where user is your username and group is often the same as your username.


If you don't mind trading automation for simplicity, just press the keystroke '" (apostrophe, followed by double quotes) on opening a file, you'll jump to where you were. This is essentially what @marcog's answer is doing.


  :h views-sessions

You can place this in your .vimrc :

  autocmd BufWinLeave *.* mkview
  autocmd BufWinEnter *.* silent loadview 

the views will be placed in .vim/view. You probably need to create these directories.