No edit summary |
No edit summary |
||
Line 42: | Line 42: | ||
Now the version control tools can be used like this from Vim: |
Now the version control tools can be used like this from Vim: |
||
<pre> |
<pre> |
||
− | :Git add % (The "%" expands to current filename) |
+ | :Git add % (The "%" expands to the current filename) |
:Svn diff -c 1234 |
:Svn diff -c 1234 |
||
− | :Bzr log - |
+ | :Bzr log -l 10 |
+ | :Hg annotate % |
||
</pre> |
</pre> |
||
Revision as of 16:00, 9 September 2008
Proposed tip Please edit this page to improve it, or add your comments below (do not use the discussion page).
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 the current filename) :Svn diff -c 1234 :Bzr log -l 10 :Hg annotate %