sudo bangbang
sudo bangbang

Reputation: 28199

make session settings saving function play nice with ultisnips

I'm using the following function from go away and come back script to save and load sessions when I quit and start vim. It basically saves all my settings, files in buffers etc in session.vim file in the directory where I opened vim.

function! MakeSession()
  let b:sessiondir = $HOME . "/.vim/sessions" . getcwd()
  if (filewritable(b:sessiondir) != 2)
    exe 'silent !mkdir -p ' b:sessiondir
    redraw!
  endif
  let b:filename = b:sessiondir . '/session.vim'
  exe "mksession! " . b:filename
endfunction

function! LoadSession()
  let b:sessiondir = $HOME . "/.vim/sessions" . getcwd()
  let b:sessionfile = b:sessiondir . "/session.vim"
  if (filereadable(b:sessionfile))
    exe 'source ' b:sessionfile
  else
    echo "No session loaded."
  endif
endfunction
au VimEnter * nested :call LoadSession()
au VimLeave * :call MakeSession()

Recently I added ultisnips plugin.

"Snippet engine
Plugin 'SirVer/ultisnips'
"Snippets are separated from the engine. Add this if you want them:
Plugin 'honza/vim-snippets'

Now when session.vim is created and I open vim after that, I get this error trace. This happens in insert mode when I try to edit also.

".vim/vimrc" 287L, 9566C Error detected while processing /Users/sudobangbang/.vim/bundle/ultisnips/autoload/UltiSnips.vim: line 15: Traceback (most recent call last):

Error detected while processing /Users/sudobangbang/.vim/bundle/ultisnips/autoload/UltiSnips.vim: line 15: File "", line 1, in Press ENTER or type command to continue Error detected while processing /Users/sudobangbang/.vim/bundle/ultisnips/autoload/UltiSnips.vim: line 15: ImportError: No module named UltiSnips

Error detected while processing function UltiSnips#FileTypeChanged: line 1: Traceback (most recent call last): Error detected while processing function UltiSnips#FileTypeChanged: line 1: NameError: name 'UltiSnips_Manager' is not defined

Error detected while processing function UltiSnips#TrackChange: line 1: Traceback (most recent call last):

If I remove functions for loading sessions, It works fine. Also here are all the lines in session.vim which has ultisnips in it.

inoremap <silent> <C-Tab> ^V^R=UltiSnips#ListSnippets()^V^M
xnoremap <silent> ^V   :call UltiSnips#SaveLastVisualSelection()^V^Mgvs
snoremap <silent> ^V   ^V^[:call UltiSnips#ExpandSnippet()^V^M
snoremap <silent> <C-Tab> ^V^[:call UltiSnips#ListSnippets()^V^M


set runtimepath=~/.vim,~/.vim/bundle/Vundle.vim,~/.vim/bundle/syntastic,~/.vim/bundle/nerdtree,~/.vim/bundle/vim-colorschemes,~/.vim/bundle/YouCompleteMe,~/.vim/bundle/supertab,~/.vim/bundle/ultisnips    ,~/.vim/bundle/vim-snippets,~/.vim/bundle/ctrlp.vim,~/.vim/bundle/vim-go,~/.vim/bundle/vim-commentary,~/.vim/bundle/vim-surround,~/.vim/bundle/vim-fugitive,~/.vim/bundle/vim-unimpaired,~/.vim/bundle/v    im-repeat,~/.vim/bundle/vim-airline,~/.vim/bundle/vim-airline-themes,~/.vim/bundle/gundo.vim,~/.vim/bundle/emmet-vim,~/.vim/bundle/html5.vim,~/.vim/bundle/vim-css-color,~/.vim/bundle/python-mode,~/.vi    m/bundle/vim-flake8,~/.vim/bundle/vim-ruby,~/.vim/bundle/vim-endwise,~/.vim/bundle/vim-rails,~/.vim/bundle/vim-bundler,~/.vim/bundle/vim-rake,~/.vim/bundle/vim-ruby-refactoring,~/.vim/bundle/apidock.v    im,~/.vim/bundle/blockle.vim,~/.vim/bundle/vim-rspec,~/.vim/bundle/javascript-libraries-syntax.vim,~/.vim/bundle/tern_for_vim,~/.vim/bundle/vim-javascript,/usr/local/share/vim/vimfiles,/usr/local/shar    e/vim/vim74,/usr/local/share/vim/vimfil

How can I change my session function so that vim loads ultisnips correctly?

vim --version

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Jun 4 2016 11:48:12)
MacOS X (unix) version
Included patches: 1-1864
Compiled by Homebrew

Upvotes: 5

Views: 764

Answers (2)

icc97
icc97

Reputation: 12793

I fixed this using Tim Pope's obsession.vim plugin, then the Session.vim file that gets created can be loaded in the usual way either through -S Session.vim or source Session.vim and UltiSnips works again.

In the readme this line might explain the difference:

  • [When saving the session] Don't capture options and maps. Options are sometimes mutilated and maps just interfere with updating plugins.

Here's somewhat of an explanation:

From the vim-session plugin README

Vim's :mksession command isn't really compatible with plug-ins that create buffers with generated content and because of this the vim-session plug-in includes specific workarounds for a couple of popular plug-ins:

I was pointed at the vim-session plugin by this more general SO question on plugin issues with sessions.

Upvotes: 0

sudo bangbang
sudo bangbang

Reputation: 28199

I made a fix but while sacrificing some functionality.

I removed this line from session loading

au VimEnter * nested :call LoadSession()

and made a key mapping to manually load the session

map <leader>l :call LoadSession()<CR>

Hypothesis in @Sato Katsura's comment seem to be valid.
Now I'm researching if I can go to automated part with this getting this function called when all plugins are loaded.

Upvotes: 3

Related Questions