Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

When I close a file in vim and reopen it, the file opens at the start of the file. Is there anyway to make the file open at the last place the I viewed?

share|improve this question

Put this in your .vimrc :

if has("autocmd")
  " When editing a file, always jump to the last cursor position
  autocmd BufReadPost *
  \ if line("'\"") > 0 && line ("'\"") X= line("$") |
  \   exe "normal g'\"" |
  \ endif
endif

Just change the big X with <.

If I put < the code fails to print correctly.

share|improve this answer
2  
+1. As a BTW, you can read more on this from :help last-position-jump. – DaveParillo Feb 19 '10 at 23:46
    
Also, this command ('") is not available in vi, which might be why sixtyfootersdude's vi is not behaving like this by default. It's the default behavior in vim. – DaveParillo Feb 19 '10 at 23:48
    
+1: Thanks for the response but I think that I found a simpler solution (by mistake) – sixtyfootersdude Feb 24 '10 at 15:02
up vote 6 down vote accepted

I just noticed that my cursor has started to have this behavior. I went through my vimrc (commenting out line by line) and found that this code will also work:

"make vim save and load the folding of the document each time it loads"
"also places the cursor in the last place that it was left."
au BufWinLeave * mkview
au BufWinEnter * silent loadview

(Quotes finished to make it easier to read)

The main purpose of this block is to make any folds created appear again when a file is opened but apparently it also saves/loads the cursor position.

share|improve this answer

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.

share|improve this answer
    
Hi Greg, regarding your last answers, please take a look here. – bummi Jan 17 at 7:08
    
I think its Greg is doing okay so far. Still he must go through the link you shared – The_IT_Guy_You_Don't_Like Jan 17 at 19:21
    
I think my answer is both good and relevant. I disclosed that I am the author. Is there something I've missed? From the policy: "Post good, relevant answers, and if some (but not all) happen to be about your product or website, that’s okay. However, you must disclose your affiliation in your answers." – Greg Dietsche Jan 18 at 15:46

Your Answer

 
discard

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.