Save your editing sessions

2 Points

Steven Fernandez Steven Fernandez

10 years ago

If you have multiple windows/tabs open you can save the session (ie: the state of your open files and tabs) before quitting and re-open them later.

" Session Management
function SaveSession()
    if winnr('$') > 1 || tabpagenr('$') > 1
        " we have more than one windows or tabs open, ask whether we want
        " to save the session.
        let save_sesssion = confirm("Save session ? ", "&yes\n&no", 1)
        if save_sesssion == 1
            call inputsave()
            let session_fl = input("save as: ", getcwd()."/.session.vim", "file")
            call inputrestore()
            execute 'mksession!' session_fl
        endif
    endif
endfunction

function LoadSession()
    if argc() != 0
        return
    endif
    let session_fl = getcwd()."/.session.vim"
    if filereadable(session_fl)
        let load_sesssion = confirm("Load session from '".session_fl."'?", "&yes\n&no\nload and delete\ndelete", 1)
        if load_sesssion == 1 || load_sesssion == 3
            execute 'source' session_fl
        endif
        if load_sesssion == 3 || load_sesssion == 4
            call system('unlink '.session_fl)
        endif
    endif
endfunction

au VimLeavePre * call SaveSession()
au VimEnter * call LoadSession()

Florian Beer

Florian Beer 10 years ago

This Snippet seems to be very similar to Snippet #76

Steven Fernandez

Steven Fernandez 10 years ago

Umm, yeah, it is a bit similar, I guess, although this will automatically prompt you to save sessions if you have more than one windows/tabs open when you are quitting vim and also let you enter the path to your session file without having to specifically enter a command (unlike Snippet #76).

Well, feel free to delete this one if you think it is a dup.

Florian Beer

Florian Beer 10 years ago

Ok, thanks for clearing that up. This is an improvement from my point of view.

I wont (ever) delete snippets on this site, but you could comment on Snippet #76 and explain your improvement and link to this one if you want.