syntax highlighting doesn't work after restore a previous vim session

允我心安 提交于 2019-12-12 07:46:18

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/9281438/syntax-highlighting-doesnt-work-after-restore-a-previous-vim-session

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!