Answer the question
In order to leave comments, you need to log in
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()
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"
set ssop = blank, curdir, buffers, tabpages, unix, slash, winpos, winsize, resize
filetype on
filetype plugin on
filetype indent on
syntax on
Answer the question
In order to leave comments, you need to log in
The problem is here:
autocmd VimEnter * call RestoreSession()
autocmd VimEnter * nested …
autocmd VimEnter * nested call RestoreSession()
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 questionAsk a Question
731 491 924 answers to any question