I want to configure vim to open a file at the same place I left off at.
-
4:'" apostrophe followed by double quotes redirects you last changes line – Dhiren Hamal May 05 '19 at 03:55
7 Answers
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.
-
13The autocmd comes straight out of the vim doc. See :help last-position-jump – user55400 Apr 23 '09 at 08:08
-
23If 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 03 '13 at 02: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. – Jacklynn 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
-
the `chown` solution didn't work for me, so I just uncommented the lines in `/etc/vim/vimrc` – Matt Jun 21 '17 at 22:50
-
Great answer! :-) Note however the peculiarity that that this approach does not work on files opened using marks (http://vim.wikia.com/wiki/Using_marks). If you go to a file "bookmarked" with a mark (e.g., mW), then you will not automatically return to the previous cursor/line position, if that file is opened using the shortcut `W. – Victoria Stuart Oct 29 '17 at 16:46
-
4If you don't want edit `/etc/vim/vimrc` or `/etc/vimrc` (in my case, Arch Linux) you can also add this to your `~/.vimrc` and will work in the same way (I mean, I prefer do that config in the user side, not system-wide). – jfernandz Nov 01 '19 at 12:49
-
if anyone does the same mistake I made: remove `let skip_defaults_vim=1` and `set viminfo="` from `~/.vimrc`. – Herpes Free Engineer Sep 11 '20 at 13:46
-
2Hey @jfernandz, you're comment solved my issue! I was editing `~/.viminfo` but it wasn't working. After adding the lines into `~/.vimrc`, it finally solved the issue! Many thanks! – Henrique de Sousa Jan 23 '22 at 22:23
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.

- 1,250
- 14
- 26
-
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 09:29
-
Pressing `Ctrl-c` once is enough for me. In addition, pressing `Ctrl-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 to `vim` as well. – T_T Oct 02 '19 at 02:41
: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
-
I like this answer because it also restores the scroll position unlike the accepted answer – jameh Aug 24 '21 at 19:17
-
1I needed to use `autocmd BufWinEnter *.* silent! loadview` or else every new file I opened showed an error because of a missing view file. – jameh Aug 24 '21 at 19:35
-
Use `*` instead of `*.*` for this to work with _all_ files — not only those that have a period `.` in their name – ardnew Feb 16 '22 at 23:12
-
Use `set viewdir=/path/to/views` to specify where session view scripts are saved – ardnew Feb 16 '22 at 23:33
-
@MarkHu `/etc/vim/vimrc` is for global/system-wide settings. You can find your user's `vimrc` file with `vim` command `:echo $MYVIMRC`. You may create either of these files if they do not exist. See [this answer](https://stackoverflow.com/a/8977682/1054397). – ardnew Feb 16 '22 at 23:43
-
I don't like this answer because it also saves the vim configuration, so you need to source the new vimrc in each file you reopen – kaios May 11 '22 at 09:48
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.

- 862
- 8
- 16
-
2all you can do without plugins is best. aim for .vimrc: the smaller the code the lighter and quicker – nilon Jul 22 '17 at 06:37
-
1@nilon This isn't necessarily true. There is no observable difference in speed between 1 line of code and 100 lines of code _unless_ those 100 lines of code load things that they shouldn't (in the specific case unless the plugin doesn't contain the `after` autoload directories function). After all, plugins are nothing but code, it makes no difference whether you put it in your `.vimrc` or in an external dependency. – gented Oct 28 '22 at 11:38
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

- 374,641
- 47
- 450
- 633
-
`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 01:39
-
@alamoot no it isn't, see [vimdoc motion.txt](http://vimdoc.sourceforge.net/htmldoc/motion.html#mark-motions) - Check what character your `\`` key actually generates, or do `:verbose map \`` – sehe Oct 17 '17 at 02:37
-
1