{"thread":{"id":"15397","subject":"Extremely simple Vim interface for Git","startedAt":"2008-09-06T19:37:44Z","lastAt":"2008-09-09T06:26:12Z","messageCount":11,"participants":["Teemu Likonen","Michael Wookey","Thomas Adam","Bob Hiestand","Peter Valdemar Mørch (Lists)"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"89927","messageId":"20080906193744.GA3122@mithlond.arda.local","threadId":"15397","inReplyTo":null,"subject":"Extremely simple Vim interface for Git","fromName":"Teemu Likonen","fromEmail":"tlikonen@iki.fi","sentAt":"2008-09-06T19:37:44Z","receivedAt":"2008-09-06T19:37:44Z","isPatch":false,"sender":{"key":"tlikonen@iki.fi","avatar":null},"body":"Here's a very simple idea for using Git from Vim editor. Add these lines \nto your ~/.vimrc file:\n\n\n    command! -complete=file -nargs=* Git   call s:RunShellCommand('git '.<q-args>)\n    command! -complete=file -nargs=* Svn   call s:RunShellCommand('svn '.<q-args>)\n    command! -complete=file -nargs=+ Shell call s:RunShellCommand(<q-args>)\n\n    let $EDITOR = '/usr/bin/gvim --nofork'\n\n    function! s:RunShellCommand(cmdline)\n        botright new\n        setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile\n        setlocal nowrap\n        call setline(1,a:cmdline)\n        call setline(2,substitute(a:cmdline,'.','=','g'))\n        execute 'silent 2read !'.escape(a:cmdline,'()%#')\n        setlocal nomodifiable\n        1\n    endfunction\n\n\nNow, command :Git works just like \"git\" from shell except that the \noutput is displayed in a Vim scratch buffer/window. The buffer will be \nwiped out from memory when the window is closed. Filename completion and \npiping works. Examples:\n\n    :Git diff --cached\n    :Git help merge\n    :Git branch | column\n\n(I am aware that there are VCS plugins for Vim. I happen like this \napproach better because it works just like the command line Git which \nI'm familiar with.)\n\nAs a \"side effect\" this also adds similar :Svn command as well as :Shell \ncommand which can be used to run any shell command and have its output \ndisplayed in a Vim window. Using the first two letters of :Shell is \nenough in my system because I don't have other custom commands which \nstart with letters \"Sh\".\n\n    :Sh ls -l\n"},{"id":"89963","messageId":"d2e97e800809070332t35e4ed46u7af4653ea768b39b@mail.gmail.com","threadId":"15397","inReplyTo":"20080906193744.GA3122@mithlond.arda.local","subject":"Re: Extremely simple Vim interface for Git","fromName":"Michael Wookey","fromEmail":"michaelwookey@gmail.com","sentAt":"2008-09-07T10:32:48Z","receivedAt":"2008-09-07T10:32:48Z","isPatch":false,"sender":{"key":"michaelwookey@gmail.com","avatar":"https://avatars.githubusercontent.com/u/19476?v=4"},"body":"> Here's a very simple idea for using Git from Vim editor. Add these lines\n> to your ~/.vimrc file:\n>\n>\n>    command! -complete=file -nargs=* Git   call s:RunShellCommand('git '.<q-args>)\n>    command! -complete=file -nargs=* Svn   call s:RunShellCommand('svn '.<q-args>)\n>    command! -complete=file -nargs=+ Shell call s:RunShellCommand(<q-args>)\n>\n>    let $EDITOR = '/usr/bin/gvim --nofork'\n>\n>    function! s:RunShellCommand(cmdline)\n>        botright new\n>        setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile\n>        setlocal nowrap\n>        call setline(1,a:cmdline)\n>        call setline(2,substitute(a:cmdline,'.','=','g'))\n>        execute 'silent 2read !'.escape(a:cmdline,'()%#')\n>        setlocal nomodifiable\n>        1\n>    endfunction\n\nIf you alter the RunShellCommand() function to the following -\n\n  function! s:RunShellCommand(cmdline)\n      botright new\n      setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile\n      setlocal nowrap\n      if stridx(a:cmdline, \"diff\") > 0\n          set filetype=diff\n      endif\n      execute 'silent 0read !'.escape(a:cmdline,'%#')\n      setlocal nomodifiable\n      1\n  endfunction\n\nThen Vim will apply diff syntax highlighting to the scratch buffer\nwhen a \"diff\" command is executed.\n\nFor example:\n\n  :Git diff\n  :Git diff --cached\n\netc..\n"},{"id":"89966","messageId":"20080907131356.GC3387@mithlond.arda.local","threadId":"15397","inReplyTo":"d2e97e800809070332t35e4ed46u7af4653ea768b39b@mail.gmail.com","subject":"Re: Extremely simple Vim interface for Git","fromName":"Teemu Likonen","fromEmail":"tlikonen@iki.fi","sentAt":"2008-09-07T13:13:56Z","receivedAt":"2008-09-07T13:13:56Z","isPatch":false,"sender":{"key":"tlikonen@iki.fi","avatar":null},"body":"Michael Wookey wrote (2008-09-07 20:32 +1000):\n\n> If you alter the RunShellCommand() function to the following -\n\n>       if stridx(a:cmdline, \"diff\") > 0\n>           set filetype=diff\n>       endif\n\n> Then Vim will apply diff syntax highlighting to the scratch buffer\n> when a \"diff\" command is executed.\n\nGood idea. I implemented the same thing this way:\n\n    if match(a:cmdline,'\\v^(git|hg|svn|bzr) diff') >= 0\n        setlocal filetype=diff\n    endif\n"},{"id":"89968","messageId":"18071eea0809070623v4a54cbb8j2face0724651ef56@mail.gmail.com","threadId":"15397","inReplyTo":"20080906193744.GA3122@mithlond.arda.local","subject":"Re: Extremely simple Vim interface for Git","fromName":"Thomas Adam","fromEmail":"thomas.adam22@gmail.com","sentAt":"2008-09-07T13:23:41Z","receivedAt":"2008-09-07T13:23:41Z","isPatch":false,"sender":{"key":"thomas.adam22@gmail.com","avatar":"https://gravatar.com/avatar/137f9858bc6bfd5b2f743aefd988c81ce0cbd306248889df80e269519cfc8741?d=mp&s=160"},"body":"Hi --\n\n2008/9/6 Teemu Likonen <tlikonen@iki.fi>:\n> Here's a very simple idea for using Git from Vim editor. Add these lines\n> to your ~/.vimrc file:\n>\n>\n>    command! -complete=file -nargs=* Git   call s:RunShellCommand('git '.<q-args>)\n>    command! -complete=file -nargs=* Svn   call s:RunShellCommand('svn '.<q-args>)\n>    command! -complete=file -nargs=+ Shell call s:RunShellCommand(<q-args>)\n\nLooks interesting.  Have you seen this though:\n\nhttp://code.google.com/p/vcscommand/\n\n-- Thomas Adam\n"},{"id":"89972","messageId":"20080907135952.GD3387@mithlond.arda.local","threadId":"15397","inReplyTo":"18071eea0809070623v4a54cbb8j2face0724651ef56@mail.gmail.com","subject":"Re: Extremely simple Vim interface for Git","fromName":"Teemu Likonen","fromEmail":"tlikonen@iki.fi","sentAt":"2008-09-07T13:59:52Z","receivedAt":"2008-09-07T13:59:52Z","isPatch":false,"sender":{"key":"tlikonen@iki.fi","avatar":null},"body":"Thomas Adam wrote (2008-09-07 14:23 +0100):\n\n> 2008/9/6 Teemu Likonen <tlikonen@iki.fi>:\n> > Here's a very simple idea for using Git from Vim editor. Add these lines\n> > to your ~/.vimrc file:\n> >\n> >\n> >    command! -complete=file -nargs=* Git   call s:RunShellCommand('git '.<q-args>)\n> >    command! -complete=file -nargs=* Svn   call s:RunShellCommand('svn '.<q-args>)\n> >    command! -complete=file -nargs=+ Shell call s:RunShellCommand(<q-args>)\n> \n> Looks interesting.  Have you seen this though:\n> \n> http://code.google.com/p/vcscommand/\n\nI have tried it but I made my own because I'm familiar with git's \ncommand line interface and want to use it inside Vim without (too many) \nrestrictions. I find this approach easier because I don't need to learn \nanother interface layer.\n\nI'd like to add a hint that Vim command line expands % to current file \nname. Examples:\n\n    :Git add %\n    :Git blame -C %\n"},{"id":"89973","messageId":"18071eea0809070711m2d98e762uc33b90ad1af79368@mail.gmail.com","threadId":"15397","inReplyTo":"20080907135952.GD3387@mithlond.arda.local","subject":"Re: Extremely simple Vim interface for Git","fromName":"Thomas Adam","fromEmail":"thomas.adam22@gmail.com","sentAt":"2008-09-07T14:11:43Z","receivedAt":"2008-09-07T14:11:43Z","isPatch":false,"sender":{"key":"thomas.adam22@gmail.com","avatar":"https://gravatar.com/avatar/137f9858bc6bfd5b2f743aefd988c81ce0cbd306248889df80e269519cfc8741?d=mp&s=160"},"body":"Hello --\n\n2008/9/7 Teemu Likonen <tlikonen@iki.fi>:\n> I'd like to add a hint that Vim command line expands % to current file\n> name. Examples:\n>\n>    :Git add %\n>    :Git blame -C %\n\nI will certainly try this plugin of yours when I am at work next since\nit does look cool.  Thanks.\n\n-- Thomas Adam\n"},{"id":"89990","messageId":"20080907174650.GA3404@mithlond.arda.local","threadId":"15397","inReplyTo":"20080907131356.GC3387@mithlond.arda.local","subject":"Re: Extremely simple Vim interface for Git","fromName":"Teemu Likonen","fromEmail":"tlikonen@iki.fi","sentAt":"2008-09-07T17:46:50Z","receivedAt":"2008-09-07T17:46:50Z","isPatch":false,"sender":{"key":"tlikonen@iki.fi","avatar":null},"body":"Teemu Likonen wrote (2008-09-07 16:13 +0300):\n\n> > Then Vim will apply diff syntax highlighting to the scratch buffer\n> > when a \"diff\" command is executed.\n> \n> Good idea. I implemented the same thing this way:\n> \n>     if match(a:cmdline,'\\v^(git|hg|svn|bzr) diff') >= 0\n>         setlocal filetype=diff\n>     endif\n\nOr even better, detect by the content:\n\n    if search('\\m\\C^--- .*\\n+++ .*\\n@@','n')\n        setlocal filetype=diff\n    endif\n\nIt works with things like \"git show\" and \"git log -p\" too. Here's \nupdated version for those who are interested:\n\n    command! -complete=file -nargs=* Git call s:RunShellCommand('git '.<q-args>)\n\n    \" To run any shell command:\n    \"command! -complete=file -nargs=+ Shell call s:RunShellCommand(<q-args>)\n\n    \" Additional $VCS commands:\n    \"command! -complete=file -nargs=* Hg call s:RunShellCommand('hg '.<q-args>)\n    \"command! -complete=file -nargs=* Svn call s:RunShellCommand('svn '.<q-args>)\n    \"command! -complete=file -nargs=* Bzr call s:RunShellCommand('bzr '.<q-args>)\n\n    \" In case a shell command wants to open $EDITOR we use something\n    \" that works from inside Vim.\n    let $EDITOR = '/usr/bin/gvim --nofork'\n\n    function! s:RunShellCommand(cmdline)\n        botright new\n        setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap\n        call setline(1,a:cmdline)\n        call setline(2,substitute(a:cmdline,'.','=','g'))\n        execute 'silent $read !'.escape(a:cmdline,'()%#')\n        setlocal nomodifiable\n        if search('\\m\\C^--- .*\\n+++ .*\\n@@','n')\n            setlocal filetype=diff\n        endif\n        1\n    endfunction\n"},{"id":"90016","messageId":"d2e97e800809071547m2b2e0f82ycc370f048d826b2d@mail.gmail.com","threadId":"15397","inReplyTo":"20080907131356.GC3387@mithlond.arda.local","subject":"Re: Extremely simple Vim interface for Git","fromName":"Michael Wookey","fromEmail":"michaelwookey@gmail.com","sentAt":"2008-09-07T22:47:41Z","receivedAt":"2008-09-07T22:47:41Z","isPatch":false,"sender":{"key":"michaelwookey@gmail.com","avatar":"https://avatars.githubusercontent.com/u/19476?v=4"},"body":">> If you alter the RunShellCommand() function to the following -\n>\n>>       if stridx(a:cmdline, \"diff\") > 0\n>>           set filetype=diff\n>>       endif\n>\n>> Then Vim will apply diff syntax highlighting to the scratch buffer\n>> when a \"diff\" command is executed.\n>\n> Good idea. I implemented the same thing this way:\n>\n>    if match(a:cmdline,'\\v^(git|hg|svn|bzr) diff') >= 0\n>        setlocal filetype=diff\n>    endif\n\nVim 7.2 provides a better alternative.  If the command is specifically\n'git' then setting the filetype to 'git' also provides the correct\nsyntax highlighting.\n\nFor example,\n\n  if v:version >= 702\n      if stridx(a:cmdline, \"git\") == 0\n          setlocal filetype=git\n      endif\n  endif\n\nSyntax highlighting is displayed correctly for several commands including:\n\n  :Git diff\n  :Git show\n  :Git log\n\nAnd possibly others.  If Vim (7.2) is your default editor when\nperforming a \"git commit\" then there is also a new built in Vim\ncommand -\n\n  :DiffGitCached\n\nThis opens a preview window with the contents of \"git diff --cached\".\nThe :DiffGitCached command is only available when filetype=gitcommit.\n\nPerhaps some of these ideas should be added to the Vim wiki...\n\nhttp://vim.wikia.com/wiki/Display_shell_commands%27_output_on_Vim_window\n"},{"id":"90048","messageId":"20080908052636.GA3251@mithlond.arda.local","threadId":"15397","inReplyTo":"d2e97e800809071547m2b2e0f82ycc370f048d826b2d@mail.gmail.com","subject":"Re: Extremely simple Vim interface for Git","fromName":"Teemu Likonen","fromEmail":"tlikonen@iki.fi","sentAt":"2008-09-08T05:26:36Z","receivedAt":"2008-09-08T05:26:36Z","isPatch":false,"sender":{"key":"tlikonen@iki.fi","avatar":null},"body":"Michael Wookey wrote (2008-09-08 08:47 +1000):\n\n> Vim 7.2 provides a better alternative.  If the command is specifically\n> 'git' then setting the filetype to 'git' also provides the correct\n> syntax highlighting.\n\nThanks, I didn't know that. Definitely better. I see filetype \"git\" is \nalready in my Vim 7.1.314 (Debian Etch backports).\n\n> Perhaps some of these ideas should be added to the Vim wiki...\n> \n> http://vim.wikia.com/wiki/Display_shell_commands%27_output_on_Vim_window\n\nThis Vim tip was originally written by me. I extended it with the \nversion control tools.\n"},{"id":"90108","messageId":"cc29171c0809081041s2a4e1e2etd76b6e9b291db27e@mail.gmail.com","threadId":"15397","inReplyTo":"18071eea0809070623v4a54cbb8j2face0724651ef56@mail.gmail.com","subject":"Re: Extremely simple Vim interface for Git","fromName":"Bob Hiestand","fromEmail":"bob.hiestand@gmail.com","sentAt":"2008-09-08T17:41:36Z","receivedAt":"2008-09-08T17:41:36Z","isPatch":false,"sender":{"key":"bob.hiestand@gmail.com","avatar":null},"body":"On Sun, Sep 7, 2008 at 8:23 AM, Thomas Adam <thomas.adam22@gmail.com> wrote:\n\n> Looks interesting.  Have you seen this though:\n>\n> http://code.google.com/p/vcscommand/\n\nOn this mailing list you should use this link instead:\n\nhttp://repo.or.cz/w/vcscommand.git\n"},{"id":"90170","messageId":"48C61704.8060206@sneakemail.com","threadId":"15397","inReplyTo":"cc29171c0809081041s2a4e1e2etd76b6e9b291db27e@mail.gmail.com","subject":"Re: Extremely simple Vim interface for Git","fromName":"Peter Valdemar Mørch (Lists)","fromEmail":"4ux6as402@sneakemail.com","sentAt":"2008-09-09T06:26:12Z","receivedAt":"2008-09-09T06:26:12Z","isPatch":false,"sender":{"key":"4ux6as402@sneakemail.com","avatar":null},"body":"Bob Hiestand bob.hiestand-at-gmail.com |Lists| wrote:\n>> Looks interesting.  Have you seen this though:\n>>\n>> http://code.google.com/p/vcscommand/\n> \n> On this mailing list you should use this link instead:\n> \n> http://repo.or.cz/w/vcscommand.git\n\nI personally have the opposite preference. The home page gives an \noverview and a link to the repository. The repository has no overview or \neasy-to-find link to the home page. Therefore I'd almost always perfer a \nlink to the project home page to get an idea. For 9 out of 10 projects I \nwouldn't download the code anyway.\n\nBut who are we to impose our preferences on another poster?\n\nPeter\n-- \nPeter Valdemar Mørch\nhttp://www.morch.com\n"}]}