git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Extremely simple Vim interface for Git
@ 2008-09-06 19:37 Teemu Likonen
  2008-09-07 10:32 ` Michael Wookey
  2008-09-07 13:23 ` Thomas Adam
  0 siblings, 2 replies; 11+ messages in thread
From: Teemu Likonen @ 2008-09-06 19:37 UTC (permalink / raw)
  To: git

Here's a very simple idea for using Git from Vim editor. Add these lines 
to your ~/.vimrc file:


    command! -complete=file -nargs=* Git   call s:RunShellCommand('git '.<q-args>)
    command! -complete=file -nargs=* Svn   call s:RunShellCommand('svn '.<q-args>)
    command! -complete=file -nargs=+ Shell call s:RunShellCommand(<q-args>)

    let $EDITOR = '/usr/bin/gvim --nofork'

    function! s:RunShellCommand(cmdline)
        botright new
        setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile
        setlocal nowrap
        call setline(1,a:cmdline)
        call setline(2,substitute(a:cmdline,'.','=','g'))
        execute 'silent 2read !'.escape(a:cmdline,'()%#')
        setlocal nomodifiable
        1
    endfunction


Now, command :Git works just like "git" from shell except that the 
output is displayed in a Vim scratch buffer/window. The buffer will be 
wiped out from memory when the window is closed. Filename completion and 
piping works. Examples:

    :Git diff --cached
    :Git help merge
    :Git branch | column

(I am aware that there are VCS plugins for Vim. I happen like this 
approach better because it works just like the command line Git which 
I'm familiar with.)

As a "side effect" this also adds similar :Svn command as well as :Shell 
command which can be used to run any shell command and have its output 
displayed in a Vim window. Using the first two letters of :Shell is 
enough in my system because I don't have other custom commands which 
start with letters "Sh".

    :Sh ls -l

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Extremely simple Vim interface for Git
  2008-09-06 19:37 Extremely simple Vim interface for Git Teemu Likonen
@ 2008-09-07 10:32 ` Michael Wookey
  2008-09-07 13:13   ` Teemu Likonen
  2008-09-07 13:23 ` Thomas Adam
  1 sibling, 1 reply; 11+ messages in thread
From: Michael Wookey @ 2008-09-07 10:32 UTC (permalink / raw)
  To: Teemu Likonen; +Cc: git

> Here's a very simple idea for using Git from Vim editor. Add these lines
> to your ~/.vimrc file:
>
>
>    command! -complete=file -nargs=* Git   call s:RunShellCommand('git '.<q-args>)
>    command! -complete=file -nargs=* Svn   call s:RunShellCommand('svn '.<q-args>)
>    command! -complete=file -nargs=+ Shell call s:RunShellCommand(<q-args>)
>
>    let $EDITOR = '/usr/bin/gvim --nofork'
>
>    function! s:RunShellCommand(cmdline)
>        botright new
>        setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile
>        setlocal nowrap
>        call setline(1,a:cmdline)
>        call setline(2,substitute(a:cmdline,'.','=','g'))
>        execute 'silent 2read !'.escape(a:cmdline,'()%#')
>        setlocal nomodifiable
>        1
>    endfunction

If you alter the RunShellCommand() function to the following -

  function! s:RunShellCommand(cmdline)
      botright new
      setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile
      setlocal nowrap
      if stridx(a:cmdline, "diff") > 0
          set filetype=diff
      endif
      execute 'silent 0read !'.escape(a:cmdline,'%#')
      setlocal nomodifiable
      1
  endfunction

Then Vim will apply diff syntax highlighting to the scratch buffer
when a "diff" command is executed.

For example:

  :Git diff
  :Git diff --cached

etc..

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Extremely simple Vim interface for Git
  2008-09-07 10:32 ` Michael Wookey
@ 2008-09-07 13:13   ` Teemu Likonen
  2008-09-07 17:46     ` Teemu Likonen
  2008-09-07 22:47     ` Michael Wookey
  0 siblings, 2 replies; 11+ messages in thread
From: Teemu Likonen @ 2008-09-07 13:13 UTC (permalink / raw)
  To: Michael Wookey; +Cc: git

Michael Wookey wrote (2008-09-07 20:32 +1000):

> If you alter the RunShellCommand() function to the following -

>       if stridx(a:cmdline, "diff") > 0
>           set filetype=diff
>       endif

> Then Vim will apply diff syntax highlighting to the scratch buffer
> when a "diff" command is executed.

Good idea. I implemented the same thing this way:

    if match(a:cmdline,'\v^(git|hg|svn|bzr) diff') >= 0
        setlocal filetype=diff
    endif

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Extremely simple Vim interface for Git
  2008-09-06 19:37 Extremely simple Vim interface for Git Teemu Likonen
  2008-09-07 10:32 ` Michael Wookey
@ 2008-09-07 13:23 ` Thomas Adam
  2008-09-07 13:59   ` Teemu Likonen
  2008-09-08 17:41   ` Bob Hiestand
  1 sibling, 2 replies; 11+ messages in thread
From: Thomas Adam @ 2008-09-07 13:23 UTC (permalink / raw)
  To: Teemu Likonen; +Cc: git

Hi --

2008/9/6 Teemu Likonen <tlikonen@iki.fi>:
> Here's a very simple idea for using Git from Vim editor. Add these lines
> to your ~/.vimrc file:
>
>
>    command! -complete=file -nargs=* Git   call s:RunShellCommand('git '.<q-args>)
>    command! -complete=file -nargs=* Svn   call s:RunShellCommand('svn '.<q-args>)
>    command! -complete=file -nargs=+ Shell call s:RunShellCommand(<q-args>)

Looks interesting.  Have you seen this though:

http://code.google.com/p/vcscommand/

-- Thomas Adam

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Extremely simple Vim interface for Git
  2008-09-07 13:23 ` Thomas Adam
@ 2008-09-07 13:59   ` Teemu Likonen
  2008-09-07 14:11     ` Thomas Adam
  2008-09-08 17:41   ` Bob Hiestand
  1 sibling, 1 reply; 11+ messages in thread
From: Teemu Likonen @ 2008-09-07 13:59 UTC (permalink / raw)
  To: Thomas Adam; +Cc: git

Thomas Adam wrote (2008-09-07 14:23 +0100):

> 2008/9/6 Teemu Likonen <tlikonen@iki.fi>:
> > Here's a very simple idea for using Git from Vim editor. Add these lines
> > to your ~/.vimrc file:
> >
> >
> >    command! -complete=file -nargs=* Git   call s:RunShellCommand('git '.<q-args>)
> >    command! -complete=file -nargs=* Svn   call s:RunShellCommand('svn '.<q-args>)
> >    command! -complete=file -nargs=+ Shell call s:RunShellCommand(<q-args>)
> 
> Looks interesting.  Have you seen this though:
> 
> http://code.google.com/p/vcscommand/

I have tried it but I made my own because I'm familiar with git's 
command line interface and want to use it inside Vim without (too many) 
restrictions. I find this approach easier because I don't need to learn 
another interface layer.

I'd like to add a hint that Vim command line expands % to current file 
name. Examples:

    :Git add %
    :Git blame -C %

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Extremely simple Vim interface for Git
  2008-09-07 13:59   ` Teemu Likonen
@ 2008-09-07 14:11     ` Thomas Adam
  0 siblings, 0 replies; 11+ messages in thread
From: Thomas Adam @ 2008-09-07 14:11 UTC (permalink / raw)
  To: Teemu Likonen; +Cc: git

Hello --

2008/9/7 Teemu Likonen <tlikonen@iki.fi>:
> I'd like to add a hint that Vim command line expands % to current file
> name. Examples:
>
>    :Git add %
>    :Git blame -C %

I will certainly try this plugin of yours when I am at work next since
it does look cool.  Thanks.

-- Thomas Adam

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Extremely simple Vim interface for Git
  2008-09-07 13:13   ` Teemu Likonen
@ 2008-09-07 17:46     ` Teemu Likonen
  2008-09-07 22:47     ` Michael Wookey
  1 sibling, 0 replies; 11+ messages in thread
From: Teemu Likonen @ 2008-09-07 17:46 UTC (permalink / raw)
  To: Michael Wookey; +Cc: git

Teemu Likonen wrote (2008-09-07 16:13 +0300):

> > Then Vim will apply diff syntax highlighting to the scratch buffer
> > when a "diff" command is executed.
> 
> Good idea. I implemented the same thing this way:
> 
>     if match(a:cmdline,'\v^(git|hg|svn|bzr) diff') >= 0
>         setlocal filetype=diff
>     endif

Or even better, detect by the content:

    if search('\m\C^--- .*\n+++ .*\n@@','n')
        setlocal filetype=diff
    endif

It works with things like "git show" and "git log -p" too. Here's 
updated version for those who are interested:

    command! -complete=file -nargs=* Git call s:RunShellCommand('git '.<q-args>)

    " To run any shell command:
    "command! -complete=file -nargs=+ Shell call s:RunShellCommand(<q-args>)

    " Additional $VCS commands:
    "command! -complete=file -nargs=* Hg call s:RunShellCommand('hg '.<q-args>)
    "command! -complete=file -nargs=* Svn call s:RunShellCommand('svn '.<q-args>)
    "command! -complete=file -nargs=* Bzr call s:RunShellCommand('bzr '.<q-args>)

    " In case a shell command wants to open $EDITOR we use something
    " that works from inside Vim.
    let $EDITOR = '/usr/bin/gvim --nofork'

    function! s:RunShellCommand(cmdline)
        botright new
        setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile nowrap
        call setline(1,a:cmdline)
        call setline(2,substitute(a:cmdline,'.','=','g'))
        execute 'silent $read !'.escape(a:cmdline,'()%#')
        setlocal nomodifiable
        if search('\m\C^--- .*\n+++ .*\n@@','n')
            setlocal filetype=diff
        endif
        1
    endfunction

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Extremely simple Vim interface for Git
  2008-09-07 13:13   ` Teemu Likonen
  2008-09-07 17:46     ` Teemu Likonen
@ 2008-09-07 22:47     ` Michael Wookey
  2008-09-08  5:26       ` Teemu Likonen
  1 sibling, 1 reply; 11+ messages in thread
From: Michael Wookey @ 2008-09-07 22:47 UTC (permalink / raw)
  To: Teemu Likonen; +Cc: git

>> If you alter the RunShellCommand() function to the following -
>
>>       if stridx(a:cmdline, "diff") > 0
>>           set filetype=diff
>>       endif
>
>> Then Vim will apply diff syntax highlighting to the scratch buffer
>> when a "diff" command is executed.
>
> Good idea. I implemented the same thing this way:
>
>    if match(a:cmdline,'\v^(git|hg|svn|bzr) diff') >= 0
>        setlocal filetype=diff
>    endif

Vim 7.2 provides a better alternative.  If the command is specifically
'git' then setting the filetype to 'git' also provides the correct
syntax highlighting.

For example,

  if v:version >= 702
      if stridx(a:cmdline, "git") == 0
          setlocal filetype=git
      endif
  endif

Syntax highlighting is displayed correctly for several commands including:

  :Git diff
  :Git show
  :Git log

And possibly others.  If Vim (7.2) is your default editor when
performing a "git commit" then there is also a new built in Vim
command -

  :DiffGitCached

This opens a preview window with the contents of "git diff --cached".
The :DiffGitCached command is only available when filetype=gitcommit.

Perhaps some of these ideas should be added to the Vim wiki...

http://vim.wikia.com/wiki/Display_shell_commands%27_output_on_Vim_window

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Extremely simple Vim interface for Git
  2008-09-07 22:47     ` Michael Wookey
@ 2008-09-08  5:26       ` Teemu Likonen
  0 siblings, 0 replies; 11+ messages in thread
From: Teemu Likonen @ 2008-09-08  5:26 UTC (permalink / raw)
  To: Michael Wookey; +Cc: git

Michael Wookey wrote (2008-09-08 08:47 +1000):

> Vim 7.2 provides a better alternative.  If the command is specifically
> 'git' then setting the filetype to 'git' also provides the correct
> syntax highlighting.

Thanks, I didn't know that. Definitely better. I see filetype "git" is 
already in my Vim 7.1.314 (Debian Etch backports).

> Perhaps some of these ideas should be added to the Vim wiki...
> 
> http://vim.wikia.com/wiki/Display_shell_commands%27_output_on_Vim_window

This Vim tip was originally written by me. I extended it with the 
version control tools.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Extremely simple Vim interface for Git
  2008-09-07 13:23 ` Thomas Adam
  2008-09-07 13:59   ` Teemu Likonen
@ 2008-09-08 17:41   ` Bob Hiestand
  2008-09-09  6:26     ` "Peter Valdemar Mørch (Lists)"
  1 sibling, 1 reply; 11+ messages in thread
From: Bob Hiestand @ 2008-09-08 17:41 UTC (permalink / raw)
  To: Thomas Adam; +Cc: Teemu Likonen, git

On Sun, Sep 7, 2008 at 8:23 AM, Thomas Adam <thomas.adam22@gmail.com> wrote:

> Looks interesting.  Have you seen this though:
>
> http://code.google.com/p/vcscommand/

On this mailing list you should use this link instead:

http://repo.or.cz/w/vcscommand.git

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: Extremely simple Vim interface for Git
  2008-09-08 17:41   ` Bob Hiestand
@ 2008-09-09  6:26     ` "Peter Valdemar Mørch (Lists)"
  0 siblings, 0 replies; 11+ messages in thread
From: "Peter Valdemar Mørch (Lists)" @ 2008-09-09  6:26 UTC (permalink / raw)
  To: bob.hiestand, git

Bob Hiestand bob.hiestand-at-gmail.com |Lists| wrote:
>> Looks interesting.  Have you seen this though:
>>
>> http://code.google.com/p/vcscommand/
> 
> On this mailing list you should use this link instead:
> 
> http://repo.or.cz/w/vcscommand.git

I personally have the opposite preference. The home page gives an 
overview and a link to the repository. The repository has no overview or 
easy-to-find link to the home page. Therefore I'd almost always perfer a 
link to the project home page to get an idea. For 9 out of 10 projects I 
wouldn't download the code anyway.

But who are we to impose our preferences on another poster?

Peter
-- 
Peter Valdemar Mørch
http://www.morch.com

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2008-09-09  6:27 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-09-06 19:37 Extremely simple Vim interface for Git Teemu Likonen
2008-09-07 10:32 ` Michael Wookey
2008-09-07 13:13   ` Teemu Likonen
2008-09-07 17:46     ` Teemu Likonen
2008-09-07 22:47     ` Michael Wookey
2008-09-08  5:26       ` Teemu Likonen
2008-09-07 13:23 ` Thomas Adam
2008-09-07 13:59   ` Teemu Likonen
2008-09-07 14:11     ` Thomas Adam
2008-09-08 17:41   ` Bob Hiestand
2008-09-09  6:26     ` "Peter Valdemar Mørch (Lists)"

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).