8

Possible Duplicate:
In vim, how do I get a file to open at the same line number I closed it at last time?

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.

Community
  • 1
  • 1
static_rtti
  • 53,760
  • 47
  • 136
  • 192
  • You can set marks at various points in a file using m and return to that mark using `, but those won't persist across, say restarts of the app or closing/opening the file (I don't think). – brettkelly Nov 05 '09 at 18:22
  • 1
    There is a plugin (I am the author) called [vim-lastplace](http://github.com/dietsche/vim-lastplace) that will intelligently return you to the last edit that you made. – Greg Dietsche Nov 13 '15 at 19:40

6 Answers6

12

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
the_karel
  • 1,374
  • 12
  • 16
  • Only this solution worked for me, and believe me I tried most other popular solutions! Thanks. – quanta May 04 '20 at 19:29
  • All the plugins to do this, will all their bugs (like intermittently storing and restoring wrong cwd) replacable by this one line? Looks like that... Wow! I love it, many thanks! – Red Pill Sep 08 '21 at 10:56
  • Nope: `E576: viminfo: Missing '>' in line: au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") | exe "normal g'\"" | endif` – Henrique de Sousa Jan 23 '22 at 22:16
4

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
  • 53,118
  • 13
  • 86
  • 159
3

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
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
TinaMarie
  • 121
  • 3
2

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

JT.
  • 71
  • 2
  • 1
0

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 ``` 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
  • 8,232
  • 9
  • 43
  • 54
0

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
  • 249,864
  • 45
  • 407
  • 398