tags:

views:

156

answers:

6

How do you make Vim take you back where you were when you last edited a file?

My work computer has this feature, but not my home computer! How do you set Vim to remember in which part of a file you were when you last edited it?

EDIT: just to be more precise, I want this behavior when opening a new file, or on startup.

+2  A: 

'0 // (single quote followed by zero) take you to place you last edited

JT
If you can tell me how to execute this automatically each time a buffer is open, you get the best answer :)
static_rtti
+2  A: 

First, check that your .vimrc file is writable.

If that isn't sufficient, add this to your .vimrc:

if has("autocmd")
    autocmd BufReadPost *
    \ if line("\'") > 0 && line("\'") <= line("$") |
        \ exe "normal g`" |
    \ endif
endif
TinaMarie
could you please post in "code"? Stack overflow seems to be doing weird character substitutions...
static_rtti
The weird quotes are still there...
static_rtti
This solution won't work anyway. See my response.
Ether
+2  A: 

This is done with the viminfo file. It should be sufficient simply to enable this feature (and ensure that the file is writable). I use:

set viminfo='25,\"50,n~/.viminfo

...which stores viminfo data into ~/.viminfo. You can read about the other customization options here.

Ether
I do have a viminfo file, filled with all kinds of stuff. It just doesn't seem to work.
static_rtti
A: 

This is a dirty solution.

Whenever you are done editing a file and you are going to quit, just mark that place using m<letter>. Now you create a session file for this using mks!. Next time you open this file, just do `<letter> to reach that place.

You can have different letters to reach different places in the file. e.g. e for the place you were last editing or f function definition you were last working on etcetera. It depends upon your taste.

One thing bad about this solution is that mks! will create a Session.vim file in the current directory.

Waseem
A: 

I have this in my .vimrc and it works:

" go to the position I was when last editing the file
au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g'\"" | endif
sessy
A: 

See also the Vim Wikia article on Restore Cursor to File Position in Previous Editing Session. It looks like it lists a couple of the solutions already listed here, in addition to one larger script that specifically stores the last position for the last 10 files you've edited; you can change that value to suit your needs.

Mark Rushakoff