1

Marks are saved in .viminfo

I would like to have separate marks saved per session.

Is there an easy setting to make this switch? I know I could write some vimscript to save my session marks somewhere and reload them to overwrite whatever viminfo is doing, but I don't want to spend the time if I don't have to.

I'm particularly interested in the A-Z marks. I often have a few sessions open for different projects and some functions that help me jump between them quickly, and to my chagrin I discovered viminfo, not sessions, is where marks are saved. I see the use of this, but it doesn't fit my workflow.

1 Answer 1

2

Is there an easy setting to make this switch?

Nope

I know I could write some vimscript to save my session marks somewhere and reload them to overwrite whatever viminfo is doing, but I don't want to spend the time if I don't have to.

That is exactly how you would do this.

Example code:

command! -bang -complete=file -nargs=? Mksession mksession<bang> <args> |
      \ let [s:viminfofile, s:viminfo] = [&viminfofile, &viminfo] |
      \ try |
      \   let [&viminfofile, &viminfo] = [v:this_session . '.viminfo', 'f'] |
      \   wviminfo! |
      \ finally |
      \   let [&viminfofile, &viminfo] = [s:viminfofile, s:viminfo] |
      \ endtry

augroup ViminfoSessionRestore
  autocmd!
  autocmd SessionLoadPost * |
        \ let [s:viminfofile, s:viminfo] = [&viminfofile, &viminfo] |
        \ try |
        \   let [&viminfofile, &viminfo] = [v:this_session . '.viminfo', 'f'] |
        \   rviminfo! |
        \ finally |
        \   let [&viminfofile, &viminfo] = [s:viminfofile, s:viminfo] |
        \ endtry
augroup END

NOTE: I have not tested this, so use at your own risk.

This provides :Mksession which will write out a new viminfo files named after the v:this_session variable with a suffix of .viminfo. This viminfo file only contains file marks (f option). After a session is loaded the SessionLoadPost event is triggered. Attempt to read the session's viminfo file via :rviminfo on SessionLoadPost event.

Other thoughts:

I would imagine creating another file in addition to the session file would become annoying. It might be better to append the file-mark data to the session file directly, but that would be more involved.

You may also want to look into some session plugins to make your session creation and management simpler, e.g. obsession.vim & VIM ProSession.

For more help see:

:h 'viminfo'
:h 'viminfofile'
:h this_session
:h SessionLoadPost
:h :mksession
:h :rviminfo
:h :wviminfo
3
  • Thanks. I just used set sessionoptions+=globals with a dictionary variable and a stringified dictionary variable (sessions can't save dicts) and loop through letters using getpos and setpos, and the autocmd SessionLoadPost to run on startup with delmarks A-Z to get rid of the viminfo stuff. I would post it, but I'll probably run into a bug or two as I use it. I think your idea could have unforeseen consequences, and touches more than it needs to. It took me about an hour, it's too bad viminfo doesn't have more options since many times with vim you do have lots of options.
    – Wumbo
    Aug 14, 2018 at 15:52
  • Hey! this is exactly what I was looking for! Create one viminfo file per session. Haven't been able to make it work though. Have any suggestions to share?
    – lalo
    Aug 23, 2019 at 15:38
  • The :Mksession should make a file named after the session file. e.g :Mksession ~/Session.vim should yield ~/Session.vim.viminfo. I am not sure about the : rviminfo & 'viminfofile' working together in the ViminfoSessionRestore event. I do not use sessions personally. If you come up with a solution please share and we can add it to this question Aug 23, 2019 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.