In vim, how do I get a file to open at the same line number I closed it at last time?
Asked Answered
vim
V

9

178

I want to configure vim to open a file at the same place I left off at.

Villainous answered 21/4, 2009 at 20:56 Comment(1)
:'" apostrophe followed by double quotes redirects you last changes lineGleeman
G
243

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.

Glyph answered 21/4, 2009 at 21:3 Comment(11)
The autocmd comes straight out of the vim doc. See :help last-position-jumpOedipus
If 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`\"" | endifLecroy
The chown part fixed the problem for me. Hats off!Hathcock
Had the setting but the permissions note made it work. Thanks.Hammel
Finally 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.Bookish
Just 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.Enabling
the chown solution didn't work for me, so I just uncommented the lines in /etc/vim/vimrcPreliminaries
Great answer! :-) Note however the peculiarity that that this approach does not work on files opened using marks (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.Otolaryngology
If 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).Kevenkeverian
if anyone does the same mistake I made: remove let skip_defaults_vim=1 and set viminfo=" from ~/.vimrc.Chesnut
Hey @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!Cardinalate
Z
66

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.

Zach answered 22/4, 2009 at 6:37 Comment(0)
S
30

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.

Slander answered 24/11, 2017 at 22:46 Comment(2)
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.Breakable
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.Chemo
E
29

: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.

Etude answered 22/4, 2009 at 6:21 Comment(7)
I like this answer because the other one referred to an /etc/vim/ file that did not exist on my Mac.Smarm
I like this answer because it also restores the scroll position unlike the accepted answerPositronium
I needed to use autocmd BufWinEnter *.* silent! loadview or else every new file I opened showed an error because of a missing view file.Positronium
Use * instead of *.* for this to work with all files — not only those that have a period . in their nameIncinerator
Use set viewdir=/path/to/views to specify where session view scripts are savedIncinerator
@Smarm /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.Incinerator
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 reopenCasement
I
9

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.

Ingrain answered 17/1, 2016 at 3:19 Comment(2)
all you can do without plugins is best. aim for .vimrc: the smaller the code the lighter and quickerDastardly
@Dastardly 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.Thinia
A
7

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

Arin answered 30/3, 2011 at 7:34 Comment(3)
`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?Tractor
@Tractor no it isn't, see vimdoc motion.txt - Check what character your ` key actually generates, or do :verbose map `Arin
you are right. According to the vimdoc both are correct!Tractor
R
2

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.

Rm answered 15/9, 2017 at 10:29 Comment(0)
F
2

At this point in time :help restore-cursor should be able to get what you want.

Fur answered 10/11, 2023 at 19:11 Comment(0)
T
0
augroup resumefile
    autocmd!
    autocmd BufReadPost * if line("'\"") > 0 && line("'\"") <= 
 line("$") | exe "normal! g`\"" | endif
augroup END
Trossachs answered 4/4 at 5:40 Comment(1)
Welcome to StackOverflow. Please, edit and try for How to Answer, describe the effect of what you propose and explain why it helps to solve the problem. Find help with formatting your post here: stackoverflow.com/help/formatting . Consider taking the tour.Brewage

© 2022 - 2024 — McMap. All rights reserved.