Suppose I have the following text in my vim buffer:
This is a commit msg.
Suppose further I have a git repo located at ~/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
. It would look something like
:'<,'>Commit ~/my_repo
It would also have auto-complete on its repo argument.
First, the autocomplete function (AFAIK I think this is OK?):
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:
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):
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