Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
723 views
in Technique[技术] by (71.8m points)

vim - vimrc auto-commit w/message prompt

I use the following command in my vimrc to auto commit on save. I find this very useful. However I do not like that I am stuck with the same commit message every time.

autocmd BufWritePost * execute ':silent ! if git rev-parse --git-dir > /dev/null 2>&1 ; then git add % ; git commit -m "Auto-commit: saved %"; fi > /dev/null 2>&1'

What I would like is to receive a prompt when saving that allows me to either provide a commit message or press enter and use the "Auto-commit: saved %" as a default when I am in a hurry.

I played around with input() and didnt have any luck within this particular command.

I also attempted to use a value returned by a function but could not get that to work either.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

input() is a built-in function, you assign its result to a variable, and can then insert (with proper escaping) its contents into your external shell command:

autocmd BufWritePost * let message = input('Message? ', 'Auto-commit: saved ' . expand('%')) | execute ':silent ! if git rev-parse --git-dir > /dev/null 2>&1 ; then git add % ; git commit -m ' . shellescape(message, 1) . '; fi > /dev/null 2>&1'

This one will query on every save. With an added conditional, you could make it abort the commit when no message is given.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...