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.

Leave a Comment