views:

49

answers:

2

I find I waste a lot of time closing and reopening sets of files so I'd like to improve my VIM macro for loading and saving the session to support multiple sessions.

I'd like for it to prompt for a string value, so that I could press my shortcut, then type in for example "foo", and have my macro save the session to .foo (so I also need to do basic string concat on it). Then I'd do the same for the load macro and manage sessions by theme (using MVC framework you tend to have a lot of files to work with).

" Control-S to save and Shift F5 to load
set sessionoptions=tabpages,winpos
map <S-F5> :source ~/.vim/.session<cr>
map <c-s> :mksession! ~/.vim/.session<cr>\| :echo "Session saved."<CR>

I have very little experience of VIM scripting. Is it possible to do this in a one liner, or perhaps a small function?

Thank you.

+1  A: 
map <s-f5> :execute "source ".input("session name: ", "~/.vim/session.", "file")<cr>

Enter "foo" to load "session.foo".

Instead, you can also do:

map <s-f5> :source ~/.vim/session.

Note there isn't a <cr>, so you complete the command yourself and press enter — identical typing as above, even down to filename completion.

However, I'd look at calling a function or something else entirely at about this point.

Roger Pate
Awesome, thank you!
faB
@faB I suggest adding `"file"` as a third argument to `input` in the first mapping to get filename completion.
ZyX
Great, I'll try that, cheers.
faB
A: 

Here's the snippet I have now, in case someone needs something similar (no need to vote). It saves sessions under .session.xyz which are also excluded from my Git project. I like to store them in the Git project folder so they are saved with backups.

I like confirmation echo as well because when you press enter after saving the session otherwise you can't see that anything happened. It's just for feedback.

 map <S-F5> :execute "source ".input("Load session: ", "~/Some/Project/.session.", "file")<cr>
 map <c-s> :execute "mksession! ".input("Save session: ", "~/Some/Project/.session.", "file")\| :echo "Session saved."<CR>

The file completion makes this very handy, thank you!

faB