0

I used exvim as session manager, it uses a project file with extension .exvim. The session is auto-loaded when double click the .exvim, and auto-saved on exit. But there're bugs and the dev is inactive for years, now I switch to use vim-session.

I like the '.exvim' idea, I'm trying to implement '.proj' with vim-session in my vimrc:

let g:session_autosave = 'yes' "auto saved
let g:session_autoload = 'no' "load by myself as below
function! s:open_proj_session()
    " if the buffer is /a/b/mars.proj, just get the 'mars'
    let session_name = fnamemodify(fnamemodify(bufname('%'), ":t"), ':r')
    " try to open it first
    call xolox#session#open_cmd(session_name, '', 'OpenSession') 

    " if it's not opened then it's not created, let's create it
    let cur_sess = xolox#session#find_current_session()
    if cur_sess != session_name
        call xolox#session#save_cmd(session_name, '', 'SaveSession')
    endif
endfunction
augroup MyProj
    autocmd!
    autocmd! BufRead,BufNewFile *.proj set filetype=proj "register .proj filetype
    autocmd FileType proj nested call s:open_proj_session()
augroup END

Once I open a .proj file, session loaded successfully, but buffers has no filetype, try to print filetype in some .cpp buffer with :set filetype, results in: filetype=conf or filetype=. And the syntax highlight is not working.

I see the official dev is also inactive for years so I decide to post it here, how to fix this?

Problem Update:

Code updated, add a nested keyword works. Now only the active buffer not syntax highlighted. Its ``filetype=cpp` is already set, but has no syntax('No Syntax items defined for this buffer'), all other buffer are syntax highlighted, why?

4
  • 1
    Have you tried to put the comment "register .proj filetype on another line to be sure that it doesn't mess with the autocommand? (Also I think fnamemodify(fnamemodify(bufname('%'), ":t"), ':r') could be expand('%:t:r'))
    – statox
    Jul 30, 2018 at 7:57
  • 1
    @statox The expand('%:t:r') is helpful, and it has the same result if I delete the comments
    – aj3423
    Jul 30, 2018 at 12:46
  • 2
    I think you might need the nested flag to your autocommands Aug 1, 2018 at 7:53
  • @ChristianBrabandt The nested flag works, but still have some problem, the thread updated for this.
    – aj3423
    Aug 1, 2018 at 14:13

1 Answer 1

0

This works fine:

Plug 'xolox/vim-session'
let g:session_autosave = 'yes' "auto saved
let g:session_autoload = 'no' "load by myself as below
function! s:open_proj_session()
    let sess_name = expand('%:t:r')

    let &titlestring = sess_name
    call xolox#session#open_cmd(sess_name, '', 'OpenSession') " open it first

    let cur_sess = xolox#session#find_current_session() " if it's not opened then it's not created, let's create it
    if cur_sess != sess_name
        call xolox#session#save_cmd(sess_name, '', 'SaveSession')
    endif

    " close the .proj buffer
    if bufnr(sess_name.'.proj') > 0
        exec 'bd '.sess_name.'.proj'
    endif

    "close the .vim buffer (session file)
    if bufnr(sess_name.'.vim') > 0
        exec 'bd '.sess_name.'.vim'
    endif
endfunction
augroup MyProj
    autocmd!
    autocmd! BufEnter *.proj nested call s:open_proj_session()
augroup END
set ssop+=resize,winpos,winsize,blank,folds

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.