Vim Tips Wiki
No edit summary
No edit summary
Line 20: Line 20:
 
call setline(1,a:cmdline)
 
call setline(1,a:cmdline)
 
call setline(2,substitute(a:cmdline,'.','=','g'))
 
call setline(2,substitute(a:cmdline,'.','=','g'))
execute 'silent 2read !'.escape(a:cmdline,'%#')
+
execute 'silent $read !'.escape(a:cmdline,'%#')
 
setlocal nomodifiable
 
setlocal nomodifiable
 
1
 
1
Line 34: Line 34:
   
 
<pre>
 
<pre>
command! -complete=file -nargs=* Git call s:RunShellCommand('git '.<q-args>)
+
command! -complete=file -nargs=* Git call s:RunShellCommand('git '.&lt;q-args&gt;)
command! -complete=file -nargs=* Svn call s:RunShellCommand('svn '.<q-args>)
+
command! -complete=file -nargs=* Svn call s:RunShellCommand('svn '.&lt;q-args&gt;)
command! -complete=file -nargs=* Bzr call s:RunShellCommand('bzr '.<q-args>)
+
command! -complete=file -nargs=* Bzr call s:RunShellCommand('bzr '.&lt;q-args&gt;)
command! -complete=file -nargs=* Hg call s:RunShellCommand('hg '.<q-args>)
+
command! -complete=file -nargs=* Hg call s:RunShellCommand('hg '.&lt;q-args&gt;)
 
</pre>
 
</pre>
   

Revision as of 05:36, 8 September 2008

Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).

Please use new tips to discuss whether this page should be a permanent tip, or whether it should be merged to an existing tip.
created August 4, 2008 · complexity basic · author Anon · version 7.0

The :! command is useful for running shell commands from Vim. It has one possible drawback: the command output is not displayed in a Vim window, so it can't be accessed with Vim's powerful editing tools. This is easy to fix with :read !command which inserts the output to the current window. A possibly more sophisticated solution is to make a new Vim command which opens a scratch buffer for the output of shell command. Here's an example:

command! -complete=file -nargs=+ Shell call s:RunShellCommand(<q-args>)
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
  1
endfunction

The new :Shell command works just like :! except that it opens a Vim scratch buffer and prints the command output there. The scratch buffer will be wiped out completely when it's closed. An example of usage:

:Shell ls -la

The idea can be extended by adding shortcut commands for commonly used shell commands. This example adds some common version control tools:

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=* Bzr call s:RunShellCommand('bzr '.<q-args>)
command! -complete=file -nargs=* Hg  call s:RunShellCommand('hg '.<q-args>)

Now the version control tools can be used like this from Vim:

:Git add %               # The % expands to current filename
:Svn diff -c 1234
:Bzr log -l10

Comments