A
A
Andrey Caramba2011-11-08 10:41:27
vim
Andrey Caramba, 2011-11-08 10:41:27

Syntax highlighting crashes when restoring a session in Vim?

I'm just starting to learn Vim and I wanted to set it up in such a way that when it starts from a specific directory (for example, the project directory), it restores all previously opened files for this very directory.
To do this, I wrote the following in _vimrc

function! SessionDir()
    if has('win32')
        let sessiondir = $VIM . '/vimfiles/sessions/'.
            \substitute(fnamemodify(".",":pwd"), ':', '', '')  
    else
        let sessiondir = $HOME . '/.vim/sessions/'.
            \substitute(fnamemodify(".",":pwd"), '^'.$HOME, '~', '')           
    endif

    return sessiondir
endfunctio

function! SaveSession()
    let sessiondir = SessionDir()

    " Create session directory if it doesn't exist
    if !isdirectory(sessiondir)
        call mkdir(sessiondir, 'p', 0700)
    endif

    execute "mksession! " . sessiondir . "/session.vim"
endfunction

function! RestoreSession()
    let sessiondir = SessionDir()

    if argc() == 0 && filereadable(sessiondir . "/session.vim")
        execute "source " . sessiondir . "/session.vim"
    endif
endfunction

autocmd VimLeave * call SaveSession()
autocmd VimEnter * call RestoreSession()

Sessions seem to be saved and restored as they should, but when the session is restored, the syntax highlighting for all already open files flies. And :set filetype? returns an empty string.
Before that, it simply saved the current session, without being tied to the Vim launch directory. The code in _vimrc was like this
autocmd VimLeave * execute "mksession! " . $VIM . "/vimfiles/session.vim"

autocmd VimEnter * nested if argc() == 0 && filereadable($VIM . "/vimfiles/session.vim") |
    \ execute "source " . $VIM . "/vimfiles/session.vim"

And there were no problems.
Session options are expelled in
set ssop = blank, curdir, buffers, tabpages, unix, slash, winpos, winsize, resize

Options required for syntax highlighting included
filetype on
filetype plugin on 
filetype indent on
syntax on

I apologize in advance for such a noobish code and question.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
ZyXI, 2012-09-12
@andycaramba

The problem is here:

autocmd VimEnter * call RestoreSession()

before that you had it right
autocmd VimEnter * nested …

. The bottom line is that with the new command you disable nested events in Vim, while the old one was allowed. Determining the file type, loading the syntax and appropriate additions is done on an event, so you need to add nested right after the template:
autocmd VimEnter * nested call RestoreSession()

K
klen, 2011-11-08
@klen

I implemented a similar mechanism, with one exception, the autosaved session is not loaded automatically, but still on command (key combinations). To restore the settings after the session is loaded, I make a `source vimrc`.
You can see my settings here: github.com/klen/.vim/blob/master/rc.vim

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question