vim: Jump to the last known cursor when file open

May 21st, 2010 mysurface Posted in gvim | Hits: 169394 | 3 Comments »

When dealing with source codes by using vim, I usually open and close the files repeatably. It will be good if vim can automatically jump me to the last known position when I reopen a file.

To have vim do so, you must have viminfo, which usually exist by default.

Add in the setting into your ~/.vimrc


if has("autocmd")
  autocmd BufReadPost *
    \ if line("'\"") > 1 && line("'\"") <= line("$") |
    \   exe "normal! g`\"" |
    \ endif
endif

Ok, I have no idea what how it does the magic, but I know if your vim editor have autocmd capability, it will do wonders.

P.S. For gvim users, you can edit the vimrc by accessing the menu Edit > Startup Settings from the gvim.

3 Responses to “vim: Jump to the last known cursor when file open”

  1. But, by this solution, it just jumps to the “line” but not the exact cursor position where you left.

  2. Probably, but it is sufficient for me :)

  3. I’m now not sure where you’re getting your information, but good topic.
    I must spend a while studying more or figuring out more.
    Thank you for excellent info I was on the lookout for this information for my mission.

Leave a Reply