Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 333
devEvan Avatar asked Feb 14 '12 17:02

devEvan


People also ask

How do I permanently set syntax in Vim?

Add the text, “syntax on” anywhere in the file to enable syntax highlighting permanently for vim editor. Save and close the file by typing ':x'. For disabling the feature, just re-open . vimrc file, change the text “syntax on” to “syntax off” and save the file.

What Vim configuration option will activate syntax highlighting?

To enable Syntax Highlighting feature in VI editor, open the file called /etc/profile. Add the alias function to VI by pointing to VIM in /etc/profile file. This file is used to set alias functions globally. If you would like to set user specific aliases and functions, then you need to open the file .

How do I save a Vim session?

To Save a Session ( :mksession , :mks )vim file on the directory where the current Vim execution was started. Using exclamation mark, :mks! the command overwrites Session. vim file if it exists in the directory. You can specify a custom file name to save the session :mks!

How do I enable syntax highlighting in Vim Mac?

If you want to toggle this on/off (without creating a . vimrc file) simply type :syntax on while in vi/vim.


2 Answers

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
like image 62
titusd Avatar answered Sep 18 '22 16:09

titusd


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.

like image 25
stuckprogrammersad Avatar answered Sep 21 '22 16:09

stuckprogrammersad