开发者

VIM prompting for a variable when running a macro?

开发者 https://www.devze.com 2023-01-20 15:50 出处:网络
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 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.


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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消