Sign up ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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.

share|improve this question
I am sorry, but there seems to be 0 documentation that says it should. Are you 100% sure thats the case for Vim ? And, i am sorry, i got no idea how to configure it to remember last position. Might an unnecessary comment by me, but i gotta know if you experienced the program doing that earlier. –  denNorske Nov 28 '12 at 13:32
"This is not the expected behavior." -- explain why. –  gertvdijk Nov 28 '12 at 14:24
For example in Redhat , the last refereed line in a file is remembered by vim. vim seems to be inconvenient in ubuntu 12.04 –  Lunar Mushrooms Nov 28 '12 at 14:34
Also in Ubuntu 10.04 this is certainly not default behaviour. I've been using Vim on Ubuntu for a long time, never saw your expected behaviour. You probably have configured it to do so on that installation - see the answer of @GaryBishop for example. –  gertvdijk Nov 28 '12 at 14:37
Sorry it was not Ubuntu 10.04 , it was red hat that worked. Updated my comment –  Lunar Mushrooms Nov 28 '12 at 14:42

4 Answers 4

There is a mistake in Lunar Mushrooms solution. Here the correction:

if has("autocmd")
  " When editing a file, always jump to the last known cursor position.
  " Don't do it when the position is invalid or when inside an event handler
  " (happens when dropping a file on gvim).
  " Also don't do it when the mark is in the first line, that is the default
  " position when opening a file.
  autocmd BufReadPost *
    \ if line("'\"") > 1 && line("'\"") <= line("$") |
    \   exe "normal! g`\"" |
    \ endif

share|improve this answer
Why do you believe your solution to be correct? It looks a lot alike, but for the backslashes at the beginning of a line. If any, shouldn't they be at the end of a line indicating the command continues on the next line...? Also, the accepted answer just talks about uncommenting a section in a global config file, that is installed together with vim. Apparently this solved the problem. Where is the error? –  Nephente Sep 29 at 9:28
Because if I copy / paste the first solution, I got an error. And I posted the content of my /usr/share/vim/vim74/vimrc_example.vim (which included the backslashes at the beginning). –  Arnaud Oct 6 at 6:01
up vote 25 down vote accepted

Resolved it :


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

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

share|improve this answer

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

share|improve this answer
This things worked in my case. I was having same issue. I have changed ownership of .viminfo (present in home directory) that fixes vim issues. But why it was created with root:root? –  Brijesh Valera May 19 at 7:22
@BrijeshValera likely because you started vim as root when the file didn't exist yet. –  Ruslan Jul 10 at 12:08
@Ruslan But then shouldn't the file have been created in root user's home directory? –  dotslash Oct 1 at 18:10
@dotslash that's tricky with sudo. It doesn't change environment (at least mostly): try running sudo bash -c 'echo $HOME', you'll get your home directory instead of root's one. –  Ruslan Oct 1 at 18:46

I think this wiki posting may provide a solution. I don't believe restoring the position is the expected behavior.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.