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?

link|improve this question

feedback

2 Answers

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.

link|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
feedback
up vote 2 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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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