vim is not remembering last position

My Ubuntu LTS 12.04 has vim editor. If I open a file, move to a paragraph and reopen vim, then the cursor goes to beginning of the file always.

This is not the expected behavior. How the vim can remember last read position after closing a file?

I tried vi also, but the result is the same.

Asked By: Lunar Mushrooms

||

I think this wiki posting may provide a solution. I don’t believe restoring the position is the expected behavior. http://vim.wikia.com/wiki/Restore_cursor_to_file_position_in_previous_editing_session

Answered By: GaryBishop

The file /etc/vim/vimrc 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

(Infact, you can refer to /usr/share/vim/vim73/vimrc_example.vim also)

Answered By: Lunar Mushrooms

I had this same problem and it turned out that the .viminfo file in my home directory had the wrong ownership. It was owned by root:root.

Once I fixed the file ownership by changing it to myself, remembering file position started working for me again

Answered By: TikiTavi

There is a mistake in Lunar Mushrooms solution. Here the correction:

if has("autocmd")
  " When editing a file, always jump to the last known cursor position.
  " Don't do it when the position is invalid or when inside an event handler
  " (happens when dropping a file on gvim).
  " Also don't do it when the mark is in the first line, that is the default
  " position when opening a file.
  autocmd BufReadPost *
     if line("'"") > 1 && line("'"") <= line("$") |
       exe "normal! g`"" |
     endif

endif
Answered By: Arnaud

There is a plugin called vim-lastplace (I am the author) that will open your files where you left off. It improves on the above suggestions by ignoring commit messages because you’re typically editing a new message and want to start at the top of the commit message file.

Answered By: Greg Dietsche

In my case, vi was a symlink: /usr/bin/vi -> /etc/alternatives/vi -> /usr/bin/vim.tiny. The latter has no real vim features. Installing the package ‘vim’ (using synaptic or apt-get) made this symlink point to /usr/bin/vim.basic, and this fixed the issue.

Answered By: Bruno Haible

Good convention is to create your own .vimrc file in your $HOME directory, and include this line which will cause Vim to jump to the last known position in a previously edited file.

In $HOME/.vimrc, add the following lines:

" Reopen the last edited position in files
au BufReadPost * if line("'"") > 1 && line("'"") <= line("$") | exe "normal! g'"" | endif

There is no need to edit the system-wide file /etc/vim/vimrc.

Creating and using your own .vimrc in your $HOME directory is also better for maintainability. The system vimrc configuration file could be updated when the Vim application package gets an update next time, or edited by someone else. Your own file in your own home-directory will be honored when you run Vim.

I actually recommend you look into all the great things you can do with your own .vimrc file. I have mine configured with tons of useful options and even a few custom functions.

Answered By: C.D.

In my case was that I had copied my .vimrc from another machine.

This was the offending line.

set viminfo='10,"100,:20,%,n~/.viminfo

My problem was that because I use different systems, ie. cygwin, wsl2, linux etc…
I had copied it from a cygwin install and it had looked something like this

set viminfo='10,"100,:20,%,n/mnt/c/cygwin/.viminfo

which is obviously not correct for my linux install.
Modding it to look like the first example above fixed my problem.

Answered By: Chai Ang

The last edit information is stored at .viminfo.

ls -l ~/.viminfo

If it hasn’t a read mod for your user, just change it.

sudo chmod 666 ~/.viminfo
Answered By: Albert Zhang

:help last-position-jump in Vim

The help for Vim actually includes documentation for how to do this. Open Vim, and run :help last-position-jump or :help restore-cursor. Here is what it says:

                *restore-cursor* *last-position-jump*  >
    autocmd BufReadPost *
       if line("'"") >= 1 && line("'"") <= line("$") && &ft !~# 'commit'
       |   exe "normal! g`""
       | endif

Another autocommand.  This time it is used after reading any file.  The
complicated stuff after it checks if the '" mark is defined, and jumps to it
if so.  The backslash at the start of a line is used to continue the command
from the previous line.  That avoids a line getting very long.
See |line-continuation|.  This only works in a Vim script file, not when
typing commands at the command-line.

If you want to make use of this code, copy and paste these documented lines into your ~.vimrc:

autocmd BufReadPost *
   if line("'"") >= 1 && line("'"") <= line("$") && &ft !~# 'commit'
   |   exe "normal! g`""
   | endif

:help last-position-jump in Neovim

Interestingly, the code in Neovim’s help files don’t match Vim’s in this regard. Here is the result of :help last-position-jump or :help restore-cursor in Neovim:

                *restore-cursor* *last-position-jump*   
    autocmd BufRead * autocmd FileType <buffer> ++once
       if &ft !~# 'commit|rebase' && line("'"") > 1 && line("'"") <= line("$") | exe 'normal! g`"' | endif

Another autocommand.  This time it is used after reading any file.  The
complicated stuff after it checks if the '" mark is defined, and jumps to it
if so.  The backslash at the start of a line is used to continue the command
from the previous line.  That avoids a line getting very long.
See |line-continuation|.  This only works in a Vim script file, not when
typing commands at the command-line.

To make use of this in NeoVim, add the following code to your init.lua file:

vim.cmd([[
    autocmd BufRead * autocmd FileType <buffer> ++once
       if &ft !~# 'commit|rebase' && line("'"") > 1 && line("'"") <= line("$") | exe 'normal! g`"' | endif
]])
Answered By: Flimm

You can save/restore your entire session to continue later even without uncommenting the BufReadPost option. the session includes not only your last cursor position but all the open files and their status.

To do that:

:mks <myfile>

You can drop the filename and the default with .ses extension will be used.

Next time launch vim with -S option:

vim -S myfile.ses

Alternatively, as long as :mks was previously used and myfile was re-saved, open your file with vim as usual and then:

:source myfile

(can also use the short form of :so myfile)

Answered By: user9101329
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.