vim is not remembering last position

My Ubuntu LTS 12.04 has vim editor. If I open a file, move to a paragraph and reopen vim, then the cursor goes to beginning of the file always.

This is not the expected behavior. How the vim can remember last read position after closing a file?

I tried vi also, but the result is the same.


Solution 1:

The file /etc/vim/vimrc already contains necessary feature. Just need to uncomment it:

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

(Infact, you can refer to /usr/share/vim/vim73/vimrc_example.vim also)

Solution 2:

I had this same problem and it turned out that the .viminfo file in my home directory had the wrong ownership. It was owned by root:root.

Once I fixed the file ownership by changing it to myself, remembering file position started working for me again

Solution 3:

I think this wiki posting may provide a solution. I don't believe restoring the position is the expected behavior. http://vim.wikia.com/wiki/Restore_cursor_to_file_position_in_previous_editing_session

Solution 4:

There is a plugin called vim-lastplace (I am the author) that will open your files where you left off. It improves on the above suggestions by ignoring commit messages because you're typically editing a new message and want to start at the top of the commit message file.