I want to configure vim to open a file at the same place I left off at.
-
3:'" apostrophe followed by double quotes redirects you last changes line – Dhiren Hamal May 5 '19 at 3:55
From Ubuntu's /etc/vim/vimrc
file, this example is commented out:
" Uncomment the following to have Vim jump to the last position when
" reopening a file
if has("autocmd")
au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$")
\| exe "normal! g'\"" | endif
endif
If this doesn't work, a common problem is not having ownership of your ~/.viminfo
file. If this is the case, then run:
sudo chown user:group ~/.viminfo
where user is your username
and group
is often the same as your username.
-
9The autocmd comes straight out of the vim doc. See :help last-position-jump – user55400 Apr 23 '09 at 8:08
-
20If the single quote in the penultimate line is changed to a backtick, it will jump to the actual cursor position, not just the beginning of that line:
\| exe "normal! g`\"" | endif
– u2622 Jun 3 '13 at 2:40 -
4
-
2Finally someone points out that my .viminfo is owned by root for some reason! This needs to be in the other 100 documentations that I read. – Jack May 29 '15 at 22:19
-
2Just want to +1 the
chown user ~/.viminfo
addition. I've been trying to figure this out for an hour now and you just saved my day. – Christopher Reid Jan 25 '16 at 22:56
If you don't mind trading automation for simplicity, just press the keystroke '" (apostrophe, followed by double quotes) on opening a file, you'll jump to where you were. This is essentially what @marcog's answer is doing.
:h views-sessions
You can place this in your .vimrc :
autocmd BufWinLeave *.* mkview
autocmd BufWinEnter *.* silent loadview
the views will be placed in .vim/view. You probably need to create these directories.
-
1I like this answer because the other one referred to an /etc/vim/ file that did not exist on my Mac. – MarkHu Oct 19 '17 at 18:29
You can start vim without specifying a file name using
vim
Next press CTRL+O twice to move to the last location in any file you worked on.
-
If you have NERDTree installed, pressing C-o twice can open NERDTree in current buffer. In my case, I sometimes need to press C-o up to five times, but eventually I get the effect described by @MatAff. – ashrasmun Aug 25 '19 at 9:29
-
Pressing
Ctrl-c
once is enough for me. In addition, pressingCtrl-c
more takes me further back in the cursor position histroy (even opening relevant unopened files). I use Neovim 0.4.2, but my understanding is that it applies tovim
as well. – T_T Oct 2 '19 at 2:41
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.
-
1all you can do without plugins is best. aim for .vimrc: the smaller the code the lighter and quicker – nilon Jul 22 '17 at 6:37
If you have viminfo enabled, it is as simple as `0
to go to the last edited file position. You'll notice that this is just a 'go to mark' command;
Indeed, you can later do '3 to go to the third previous edited location (perhaps in another file), and then return to the last one with `0
again
Have a look at
:marks
to see remembered locations. Note also that viminfo stores all kinds of other stuff (like the contents of registers, marks per file, command and search history). Most people have this enabled for obvious reasons
-
`0 does not work for me on vim version 7.4.1689 on mac. But '0 does the trick. Is this a typo in the answer? – alamoot Oct 17 '17 at 1:39
-
@alamoot no it isn't, see vimdoc motion.txt - Check what character your
`
key actually generates, or do:verbose map `
– sehe Oct 17 '17 at 2:37 -
Sometimes ~/.viminfo
becomes read-only or your user don't have access to the file. That could also be a reason that vim does not store your cursor position when you close your file.