120

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.

muru
  • 197,895
  • 55
  • 485
  • 740
  • 4
    For example in Redhat , the last refereed line in a file is remembered by vim. vim seems to be inconvenient in ubuntu 12.04 – Lunar Mushrooms Nov 28 '12 at 14:34
  • Also in Ubuntu 10.04 this is certainly not default behaviour. I've been using Vim on Ubuntu for a long time, never saw your expected behaviour. You probably have configured it to do so on that installation - see the answer of @GaryBishop for example. – gertvdijk Nov 28 '12 at 14:37
  • Sorry it was not Ubuntu 10.04 , it was red hat that worked. Updated my comment – Lunar Mushrooms Nov 28 '12 at 14:42
  • 1
    I am sorry, but there seems to be 0 documentation that says it should. Are you 100% sure thats the case for Vim ? And, i am sorry, i got no idea how to configure it to remember last position. Might an unnecessary comment by me, but i gotta know if you experienced the program doing that earlier. – denNorske Nov 28 '12 at 13:32
  • Related: https://stackoverflow.com/questions/774560/in-vim-how-do-i-get-a-file-to-open-at-the-same-line-number-i-closed-it-at-last – David Cary Nov 03 '21 at 22:36

9 Answers9

157

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)

αғsнιη
  • 35,660
  • It only seems to work with root user, not other users. My vimrc file is -rw-r--r-- 1 root root; would that be the problem? – ADTC Dec 11 '16 at 08:01
  • 3
    I was having the odd problem where one user would remember last position, but no others. Turns out that these lines were in the user's .vimrc file, but were commented out in /etc/vim/vimrc. Uncommenting them there enabled it for all users. – felwithe Aug 20 '17 at 15:34
  • This solution also works with gVim on Windows (where the file is "_vimrc" in your user directory). – felwithe Apr 24 '20 at 20:16
  • 2
    Editing your system's configuration files (in the /etc/vim/ folder) is not good convention. It would be better to add this line to your own .vimrc config file in your $HOME directory. See my answer for details if needed. – C.D. Aug 13 '20 at 23:26
  • 2
    I like to add a zz, to center on that point: exe "normal! g'\"zz" – Sam Watkins Apr 30 '21 at 03:53
  • @SamWatkins, you are amazing! Came here looking for exactly what you posted. Such a great addition! – Kenny Pyatt May 28 '22 at 17:57
  • works on macos unix too. – jimh Jul 07 '22 at 19:37
84

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

TikiTavi
  • 941
  • 6
  • 7
  • 4
    This things worked in my case. I was having same issue. I have changed ownership of .viminfo (present in home directory) that fixes vim issues. But why it was created with root:root? – Brijesh Valera May 19 '15 at 07:22
  • 1
    @BrijeshValera likely because you started vim as root when the file didn't exist yet. – Ruslan Jul 10 '15 at 12:08
  • 1
    @Ruslan But then shouldn't the file have been created in root user's home directory? – ankush981 Oct 01 '15 at 18:10
  • 5
    @dotslash that's tricky with sudo. It doesn't change environment (at least mostly): try running sudo bash -c 'echo $HOME', you'll get your home directory instead of root's one. – Ruslan Oct 01 '15 at 18:46
  • 1
    That was the cause in my 18.04 – Hanynowsky Jun 08 '18 at 10:01
6

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.

C.D.
  • 331
6

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

4

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.

0

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.

Chai Ang
  • 161
0

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.

-1

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
  • There is no reason to make the permissions 666. This is very nearly as bad as 777. 666 is writable by all. The permissions should be 600. One has no business using the .viminfo of another user I guess, but if you want to make the file readable for all, then 644 would be fine. If for some reason other users need write permission, then use groups and set 660. – Zanna Apr 06 '22 at 08:56
-1

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
Arnaud
  • 101
  • Why do you believe your solution to be correct? It looks a lot alike, but for the backslashes at the beginning of a line. If any, shouldn't they be at the end of a line indicating the command continues on the next line...? Also, the accepted answer just talks about uncommenting a section in a global config file, that is installed together with vim. Apparently this solved the problem. Where is the error? – Nephente Sep 29 '15 at 09:28
  • Because if I copy / paste the first solution, I got an error. And I posted the content of my /usr/share/vim/vim74/vimrc_example.vim (which included the backslashes at the beginning). – Arnaud Oct 06 '15 at 06:01
  • 1
    @Amaud the lines area already provided in the file, but commented. You just need to delete the " character at the beginning of lines to uncomment them. There is no need to "copy / paste the first solution". – ADTC Dec 11 '16 at 07:59