21

since dividing and loading each windows every time are kinda bothersome, I saved my session using:

mksession ~/session1.vim

and restored it using:

vim -S session1.vim

or

source session1.vim

it restores the previous session perfectly, but doesn't show any syntax highlighting at all.

I found a similar question over here: No syntax highlighting after session restore in terminal but doesn't help much.

does anyone have any idea?

Community
  • 1
  • 1
devEvan
  • 361
  • 4
  • 16
  • Need more data: What kinds of files are you reloading? Does highlighting work for any file types at all? Does your vimrc have any syntax-related commands? etc. – David Pope Feb 14 '12 at 18:50
  • they are c++ files, and my environment is Ubuntu. I'm actually using the same profiles(same vimrc, etc) for my Debian machine as well but weird thing is that it's not working only in my Ubuntu machine. – devEvan Feb 15 '12 at 17:21
  • Running :e inside vim to reload the file seems to fix the problem. There's some way to achieve this automatically? – Adrian Lopez Jul 04 '20 at 15:47

3 Answers3

27

I had the same problem; if I saved sessions without 'options' in sessionoptions, when I reloaded Vim, the buffers were reloaded, but without syntax highlighting.

The solution is to use an autocmd with nested when reloading.

Wikia has an extensive article about loading and saving sessions. The 'nested' option is mentioned at the bottom.

I use a modified version of this StackOverflow answer, here it is:

fu! SaveSess()
  execute 'mksession! ' . getcwd() . '/.session.vim'
endfunction

fu! RestoreSess()
  if filereadable(getcwd() . '/.session.vim')
    execute 'so ' . getcwd() . '/.session.vim'
    if bufexists(1)
      for l in range(1, bufnr('$'))
        if bufwinnr(l) == -1
          exec 'sbuffer ' . l
        endif
      endfor
    endif
  endif
endfunction

autocmd VimLeave * call SaveSess()
autocmd VimEnter * nested call RestoreSess()

set sessionoptions-=options  " Don't save options
Community
  • 1
  • 1
titusd
  • 516
  • 6
  • 5
  • 1
    So, not quite sure why this answer doesn't have over 1k upvotes already. Excellent work. – Elle Mundy Aug 01 '13 at 16:23
  • brilliant! So qa automatically stores the session, and vim on that directory automatically restores it! There is just one `bug`. When I `qa` and then I `vim` a particular file(s), it messes up the file(s) I want to vim with the session. It would be better if it restores the session only when no files are specified! – Paschalis May 20 '15 at 14:56
  • 1
    To do the above, make sure you nest the body of `RestoreSess` with this: `if argc() == 0` .... `endif` – Paschalis May 20 '15 at 14:59
  • @Paschalis I agree, I posted a [similar comment on the question where this code is copied from](https://stackoverflow.com/questions/5142099/how-to-auto-save-vim-session-on-quit-and-auto-reload-on-start-including-split-wi/6052704#comment76352770_6052704): I found it useful to add a check for `empty(argv())` in both `SaveSess` and `RestoreSess` to ignore the session if you specify a file parameter e.g. `vim myfile.py` otherwise I found a) you end up with `.session.vim` files all over the place, b) loading in a file with a session caused my Vim to open up all buffers in the session as windows. – icc97 Jul 06 '17 at 07:32
  • 1
    As far as I can tell the only modification from the [copied answer code](https://stackoverflow.com/a/6052704/327074) is to add `set sessionoptions-=options`, perhaps the linked answer has been modified since. – icc97 Jul 06 '17 at 07:33
  • Adding the `nested` flag to the VimEnter autocommand is the key. – Big McLargeHuge Jul 09 '21 at 16:19
2

I can across this Issue using the Obsession vim plugin and Neovim aswell. The answer in this thread helped me finding the solution although in my case the solution provided here didn't work immediately.

I took a look at the sessionoptions help page. For me the setting that fixed the problem was set sessionoptions+=localoptions. Then after reloading vim with this option in the config and after reloading the syntax highlighting, the highlighting was saved in the session.

stuckprogrammersad
  • 123
  • 1
  • 1
  • 7
0

I had the same issue. I deleted my session file, I re-created it with mks and that fixed the issue. Probably it was in an inconsistent state.

Adrian Lopez
  • 2,601
  • 5
  • 31
  • 48