Skip to content

Instantly share code, notes, and snippets.

@skanehira
Last active January 15, 2020 05:11
Show Gist options
  • Save skanehira/691c3463c6ad2d427c9a114dc46bb727 to your computer and use it in GitHub Desktop.
Save skanehira/691c3463c6ad2d427c9a114dc46bb727 to your computer and use it in GitHub Desktop.
" session
" Author: skanehira
" License: MIT
let s:save_cpo = &cpo
set cpo&vim
" buffer name
let s:session_list_buffer = 'SESSIONS'
" path separator
let s:sep = fnamemodify('.', ':p')[-1:]
"let g:session_path = expand('~/.vim/sessions')
function! s:echo_err(msg) abort
echohl ErrorMsg
echom 'session.vim' a:msg
echohl None
endfunction
function! s:files() abort
let session_path = get(g:, 'session_path', '')
if session_path is# ''
call s:echo_err('session_path is empty')
return []
endif
return readdir(session_path, '!isdirectory(v:val)')
endfunction
function! session#sessions() abort
let files = s:files()
if empty(files)
return
endif
" if buffer exists
if bufexists(s:session_list_buffer)
" if buffer display in window
let winid = bufwinid(s:session_list_buffer)
if winid isnot# -1
call win_gotoid(winid)
else
exec 'b' s:session_list_buffer
endif
else
exec 'new' s:session_list_buffer
set buftype=nofile
nnoremap <buffer>q :<C-u>bw!<CR>
nnoremap <buffer> <CR> :<C-u>call session#load_session(trim(getline('.')))<CR>
endif
" delete buffer contents
exec '%d_'
call setline(1, files)
endfunction
function! session#load_session(file) abort
exec 'source' join([g:session_path, a:file], s:sep)
endfunction
let &cpo = s:save_cpo
unlet s:save_cpo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment