I want to configure vim to open a file at the same place I left off at.
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.
\| exe "normal! g`\"" | endif
–
Lecroy chown
part fixed the problem for me. Hats off! –
Hathcock chown user ~/.viminfo
addition. I've been trying to figure this out for an hour now and you just saved my day. –
Enabling chown
solution didn't work for me, so I just uncommented the lines in /etc/vim/vimrc
–
Preliminaries /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 let skip_defaults_vim=1
and set viminfo="
from ~/.vimrc
. –
Chesnut ~/.viminfo
but it wasn't working. After adding the lines into ~/.vimrc
, it finally solved the issue! Many thanks! –
Cardinalate 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.
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.
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 :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.
autocmd BufWinEnter *.* silent! loadview
or else every new file I opened showed an error because of a missing view file. –
Positronium *
instead of *.*
for this to work with all files — not only those that have a period .
in their name –
Incinerator set viewdir=/path/to/views
to specify where session view scripts are saved –
Incinerator /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 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.
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 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
`
key actually generates, or do :verbose map `
–
Arin 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.
At this point in time :help restore-cursor
should be able to get what you want.
augroup resumefile
autocmd!
autocmd BufReadPost * if line("'\"") > 0 && line("'\"") <=
line("$") | exe "normal! g`\"" | endif
augroup END
© 2022 - 2024 — McMap. All rights reserved.