・Vimコマンドに引数を渡す方法
http://qiita.com/kasei-san/items/060bbd267ddc0cca5068
ポイントは
-nargs=? をつけとく
で関数に引数を渡す
if a:0 >= 1: echo "arg : ".a:1(引数はaでとれる)
とか
・!エクスクラメーションマークは
なにか分かってないけどつけといた
・バックグラウンドで外部コマンド実行
http://superuser.com/questions/550035/vim-execute-process-in-background
execute("! A_COMMAND")みたいにするとうまくいかなかった
call system("A_COMMAND")で期待通りに動いた.
vim上での挙動は
:call system("say hello")
で実験してみた.
・もろもろの文字のエスケープが必要
パイプとかクォーテーションとか
.vimrcに追記
command! -nargs=? C call GitCommit(<f-args>) function! GitCommit(...) if a:0 >= 1 let commitmsg = "\"" . a:1 ."\"" let currentDir = "\"". expand("%:h")."\"" let currentAbsDir= system("pwd") let syscom = "git add \-A " .currentDir. " \| git commit \-m ".commitmsg call system(syscom) else echo "Commit message should be specified!" end endfunction
使い方
:C some commit message
で,
git add -A . | git commit -m "some commit message"
に展開してバックグラウンドで実行される
追記:なんか動かなくなったので改良した.
command -nargs=? C call GitCommit(<f-args>) function GitCommit(...) if a:0 >= 1 let commitmsg = "\"" . a:1 ."\"" let currentDir = "\"". expand("%:h")."\"" let currentAbsDir= system("pwd") let syscom = "git add \-A " .currentDir. " \&\& git commit \-m ".commitmsg "let debugcom = syscom . "\&\& echo \"Committed with comment\: \"" . commitmsg ." \|\| echo \"Commit FAILED\"\." "let debugcom2 = syscom . "\&\& say success\|\| say failed " let debugcom3 = syscom . "\&\& sed \-i \-e \"s\/gitsuccess\=./gitsuccess\=1/\" \~\/\.zshrc \|\| sed \-i \-e \"s\/gitsuccess\=.\/gitsuccess\=2\/\" \~\/\.zshrc \; source \~\/\.zshrc" "echo commitmsg "echo debugcom3 call system(debugcom3) let gsuccess = $gitsuccess if gsuccess==1 echo "Commit done with message: " .commitmsg elseif gsuccess==2 echo "Git commit failed." else echo gsuccess echo "Hey, something weird happened." endif else echo "Commit message should be specified!" end endfunction