Wikia

Vim Tips Wiki

Changes: Diff the current buffer with another file

Edit

Back to page

(tip recreates a built-in command)
(Change <tt> to <code>, perhaps also minor tweak.)
 
Line 4: Line 4:
 
|previous=1341
 
|previous=1341
 
|next=1347
 
|next=1347
|created=September 26, 2006
+
|created=2006
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Michael Hooreman
 
|author=Michael Hooreman
Line 37: Line 37:
   
 
==Comments==
 
==Comments==
How does this differ from the <tt>:diffthis</tt> command?
+
How does this differ from the <code>:diffthis</code> command?
   
 
----
 
----
Line 47: Line 47:
 
</pre>
 
</pre>
   
For variety, you could try <tt>:vs</tt> instead of <tt>:sp</tt>
+
For variety, you could try <code>:vs</code> instead of <code>:sp</code>
   
 
----
 
----
I think the easiest way to diff a currently open file with another is the <tt>:diffsplit</tt> command. It loads the file and sets all the right options. Personally I like to use <tt>:vert diffsplit</tt>
+
I think the easiest way to diff a currently open file with another is the <code>:diffsplit</code> command. It loads the file and sets all the right options. Personally I like to use <code>:vert diffsplit</code>
   
 
----
 
----
Line 72: Line 72:
   
 
----
 
----
As of Vim 7, there is also <tt>:diffoff</tt> which is very similar to the DiffOff function above.
+
As of Vim 7, there is also <code>:diffoff</code> which is very similar to the DiffOff function above.
   
 
----
 
----

Latest revision as of 06:21, July 13, 2012

Tip 1343 Printable Monobook Previous Next

created 2006 · complexity intermediate · author Michael Hooreman · version n/a


This tip is deprecated for the following reasons:

As suggested in the comments, this functionality is built-in to Vim, with the :diffthis, :diffsplit, :diffoff, etc. commands

If you are already in Vim, there are two methods to do a vimdiff:

  • Run another Vim with command line vimdiff.
  • Open a new window (C-w s) and then enter appropriate commands in the two buffers.

This tip provides a function (SetDiffEnviron) to set the "diff" environment in buffer. So, if you want to diff the right and the left window, you have to run :SetDiffEnviron into the right and the left buffer.

Add the following to your vimrc file:

function! SetDiffEnviron()
  set diff
  set scrollbind
  set scrollopt=ver,jump,hor
  set nowrap
  set fdm=diff
endfunction
:command! SetDiffEnviron call SetDiffEnviron()

ReferencesEdit

CommentsEdit

How does this differ from the :diffthis command?


Might I suggest the following commands instead:

:sp <filename>
:windo diffthis

For variety, you could try :vs instead of :sp


I think the easiest way to diff a currently open file with another is the :diffsplit command. It loads the file and sets all the right options. Personally I like to use :vert diffsplit


Getting into 'diff' mode is quite easy, as other comments have shown. Turning off 'diff' mode (without quitting Vim or deleting the buffers) is more complex. I use the following function:

" Switch off diff-mode of all currently open windows.
command! DiffOff call DiffOff()
function! DiffOff()
  windo set nodiff
  windo set noscrollbind
  " Reset, then restore the 'foldmethod' and 'foldcolumn' settings by
  " re-setting the filetype. This triggers the filetype-based folding to be
  " reactivated, and ( contrary to a simple :e!) works even on modified
  " buffers.
  windo set foldmethod=manual
  windo set foldcolumn=0
  windo unlet! b:did_ftplugin | let &filetype = &filetype
endfunction

As of Vim 7, there is also :diffoff which is very similar to the DiffOff function above.


Around Wikia's network

Random Wiki