Problem:
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.
Attempted Solution:
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? :)
GitLocations
won't work as expected, since you'll be calling Vim's:find
, not thefind
command. Might try something likereturn system('find $HOME ...')
(and add a-type d
to it to skip submodules, perhaps?) – muru Feb 19 '16 at 16:19s:msg
to the contents of the line range (withinCommitTextGitRepo
above). – user1770201 Feb 19 '16 at 16:27getline()
andjoin()
like this:let s:msg = join(getline(a:l1,a:l2), "\n")
– saginaw Feb 19 '16 at 16:36