[英]Vim Function to Send Highlighted Text to a Git Commit
Suppose I have the following text in my vim buffer: 假设我的vim缓冲区中包含以下文本:
This is a commit msg.
Suppose further I have a git repo located at ~/my_repo
. 假设我还有一个git repo位于
~/my_repo
。
Goal: Make a vim script so that I can highlight the text above, and have it sent as a git commit message within ~/my_repo
. 目标:制作一个vim脚本,以便我可以突出显示上面的文本,并将其作为git commit消息发送到
~/my_repo
。 It would look something like 看起来像
:'<,'>Commit ~/my_repo
It would also have auto-complete on its repo argument. 它的repo参数也将具有自动完成功能。
First, the autocomplete function (AFAIK I think this is OK?): 首先,自动完成功能(AFAIK我认为可以吗?):
function! GitLocations()
return find $HOME -name '.git' -printf '%h\n' "generates a list of all folders which contain a .git dir
endfunction
Next, the actual git commit function, which is incomplete: 接下来,实际的git commit函数是不完整的:
function! CommitTextGitRepo(l1, l2, loc)
let s:msg = ??? " how do I make this the highlighted text from line l1 to line l2?
execute '!cd ' . a:loc . '&& git commit --allow-empty -m \"' . s:msg '\"'
endfunction
Assuming I can figure out how to get CommitTextGitRepo()
working above, the final thing I would need is this (I think): 假设我可以弄清楚如何使
CommitTextGitRepo()
在上面工作,我需要的最后一件事是(我认为):
command! -nargs=* -complete=custom,GitLocations -range Commit call CommitToGitRepo(<line1>, <line2>, <q-args>)
I'm so close. 我好亲近 How do I finish this up?
我该如何完成呢? :)
:)
join(getline(a:l1, a:l2),"\n")
should do the trick I would rather use a local variable and you might want to shellescape the message making the function close to this 应该可以解决这个问题,我宁愿使用局部变量,也可能想对消息进行脱壳,以使函数接近于此
function! CommitTextGitRepo(l1, l2, loc)
let l:msg = join(getline(a:l1,a:l2), "\n")
execute '!cd ' . a:loc . '&& git commit --allow-empty -m ' . shellescape(l:msg)
endfunction
http://vimhelp.appspot.com/eval.txt.html#shellescape%28%29 http://vimhelp.appspot.com/eval.txt.html#shellescape%28%29
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.