FallenAngel
FallenAngel

Reputation: 67

Vim return the cursor to the same point in the file when the buffer is written

The cursor is bouncing to the top of the file, but should be Returning to same point, in file, when the buffer is written? can any one see where i am going wrong?

function! ResCur() 
  if line("'\"") <= line("$")
    normal! g`"          
    return 1
  endif
endfunction

augroup resCur      
  autocmd!
  autocmd BufWriteCmd * call ResCur()
augroup END

Upvotes: 2

Views: 155

Answers (1)

ryuichiro
ryuichiro

Reputation: 3865

I should probably quote Vim FAQ:

How do I configure Vim to open a file at the last edited location?

Vim stores the cursor position of the last edited location for each buffer in the '"' register. You can use the following autocmd in your .vimrc or .gvimrc file to open a file at the last edited location:

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

For more information, read

:help '"
:help last-position-jump

Event, which triggers a call, is where problem lies. BufWriteCmd is used more to modify the behavior of write operations. But you would like to change how a file is loaded into a buffer. In this case BufReadPost, which is queued whenever an existing file is loaded into a new buffer.

Upvotes: 1

Related Questions