How do I get VIM to remember the line I was on when I reopen a file?

Add the following lines to your ~/.vimrc or global /etc/vim/vimrc

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

This will jump to the last known cursor position unless:

  • the position is invalid
  • the position is inside an event handler

This is a default configuration in /etc/vim/vimrc as mentioned vim is not remembering last position

Already contains necessary feature. Just need to uncomment it:

" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
  au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
endif

This is an auto command that looks for line numbers of the evaluated expressions. The g command jumps to the last position if it was recorded. Using :help commands for BufReadPost, line() and g` will explain the details of how this works.

Tags:

Vim