Auto-highlight parts of the text which has been changed - vim

Is it possible in VIM to highlight parts of some text the user has added/changed in a file?
Following situation demonstrates this:
user has opened a file
user has made some changes and then :w
all differences between 1 and 2 should be highlighted
user has made some changes and then :w
all differences between 1 and 4 should be highlighted
and so on.
I am looking for a most compact solution - should it be an option or a simple vim script.

You can keep a backup of the original file, then :vsplit file.bak, execute :diffthis in both buffer.

The VIM plugin vim-gitgutter will indicate lines that have been added / removed / changed if the file you are editing is part of a Git repository. This might meet your needs.

For such difference reporting, it's best to use a revision control system. The numbers from your question then map cleanly to revision numbers, and there are indeed plugins that can show those differences (also across multiple revisions, and in the form of log messages, etc.)
The vcscommand.vim - CVS/SVN/SVK/git/hg/bzr integration plugin supports all widely-used systems, and offers this in the form of a vimdiff or a unified diff in a scratch buffer. There are also plugins (vim-signify - Advanced plugin for showing VCS diffs in the SignColumn, or GitGutter for Git) that visualize this neatly in the sign column.
If you don't / can't use a revision control system, there are plugins that show the differences to the saved version (but not across saves, as this information is not available).
If you want a lightweight, Vim-only revision control system solution that can do this, have a look at my writebackup plugin and writebackupVersionControl plugin plugins, which offer vimdiffs / unified diffs, too, without requiring an underlying revision control system.

Vim tracks and maintains an undo history which records all changes made to a file. You could use a plugin like 'sjl/gundo.vim' which gives you the ability to view diffs of subsequent changes to a file (very handy).
If you keep the file under vcs, like git, you can also see diffs. For git you should use 'tpope/vim-fugitive' and then you can view diffs in several different ways, most common being :Gdiff

Related

Prevent `mergetool` and `difftool` from autosaving in `~/.vim/view`

I like Vim as my mergetool and difftool. Whenever it runs (usually from git), it saves the files view states at ~/.vim/view folder, so when back to edition, they show up with diffmode on and have weird borders. Having to disable them manually or run rm ~/.vim/view/* outside Vim and reopen the files fixes it, but seems odd. How to prevent these tools from saving missconfigured view files?
[edit, more info about my setup]:
Very raw Vim. No plugins. No mappings. Only a bunch of random convenience tweaks on .vimrc (that I believe have nothing to do with the question: personal backup of .vimrc on GitHub). That is, as #filbranden points out, it might be possible to make an if statement to differentiate a diff session from others, and only save view files when not diffing. I am afraid not to know enough about vim script at this point.

Vim file explorer with random multiple roots

One thing I like about Sublime Text is that you can drag any folder in the left panel and this folder can be expanded independently from the others. How can I achieve the same functionality in Vim?
I'm currently using NERDTree which currently supports a single root. When you open another folder it replaces the current hierarchy. So I wouldn't mind dropping this plugin in favor of another solution.
The netrw plugin that ships with Vim allows to open multiple, different splits to different locations, e.g. vertically with :Vexplore.
To make it show a recursive tree, use:
:let g:netrw_liststyle=3
I don't know any plugin that does this, but from my brief forays into NERDTree's source code, I know that modifying the plugin to do what you want is not out of the question. It would involve delving into the logic and modifying / commenting out the parts that search and re-use the current NERDTree buffer. If you're familiar with Vimscript, not a huge challenge. (Maybe you could even introduce a configuration setting for it and send it to the author for inclusion.)
It took me a while, but I managed to work something out.
I made it into a separate plugin: Vimpanel
I put a lot of other usefull things in there, apart from the "killer feature" of being able to have multiple roots.

Interactive program to selectively exclude parts of a diff file

Is there a program (preferably available on Cygwin) which I can use to "filter" a diff file interactively? i.e. I want something like git interactive add, except that I want to operate on a diff file. I have already discovered filterdiff, but I don't think that it supports interactive editing, only inclusion/exclusion of hunks based on a pre-defined search criteria.
My usage scenario: I have a patch in MQ, which I would like to split up per the tutorial here: https://www.mercurial-scm.org/wiki/MqTutorial#Split_a_patch_into_multiple_patches. In order to do so, I have to edit a patch file so that it includes only the (many) changes I want, and doing this manually with a text editor is kind of a pain.
Thanks!
You can probably get what you want using the record extension: https://www.mercurial-scm.org/wiki/RecordExtension
Apply the patch (but don't commit it) and then selectively commit chunk by chunk using record. You could do that with or without mq in the works.
Emacs’ diff-mode has commands to (un)apply or delete individual hunks of a diff. It also allows editing the hunks (keeping the hunk headers up to date automatically), and it has a hunk-split command that is a bit more powerful than that in git add -p.

TortoiseSVN : Good 'patch file' viewer?

The default patch file viewer is messy (ie. no side by side diff view etc). I tried setting the path of beyondCompare exe in "Settings->Unified Diff Viewer->custom", but beyond compare also behaves same as default diff tool.
Is there a way to atleast allow side by side diff in patch files ? If so, what is the method ?
My aim is to allow emailing of changes so that they can be reviewed before I commit them :)
Mishal
I've never found any, but the solution that I usually use is to simply apply the patch file to a pristine checkout of the tree, and then do a "regular" diff (my preferred tool is diffuse) to review the changes in context.
The problem with "raw" patch files is that they only provide a few lines of context before and after the change, which often isn't enough.
If you don't like the patch, simply revert the changes and don't commit!
Beyond Compare 4 allows you to view patch files created by SVN. The top window in Beyond Compare is a tree structure, allowing you to navigate folders and files by name. Revision numbers are present in the left and right diff windows.
Create the patch
svn diff -r 5922:6116 > CodeReview.patch
Open the patch in Beyond Compare 4
SVN 1.7 I think was released since this answer was posted, and I landed here because I wanted to show my newly created patch file with syntax coloring, a la view unified diff in TortoiseSvn.
It turns out that Notepad++ automatically syntax colors my file correctly if I give it the filetype of "patch"!
Patches have been around a long time but SVN is now supporting them more fully.
See for example the documentation;
http://tortoisesvn.net/docs/release/TortoiseSVN_en/tsvn-dug-patch.html
For how to create a patch file, see this nice document that describes various methods including mine, WinMerge;
http://docs.moodle.org/dev/How_to_create_a_patch#Creating_a_patch_using_WinMerge
If you have access to a Mac OS X machine, PatchViewer sounds like it might do what you're looking for:
http://appledeveloper.com.au/products/patchviewer/
(Disclaimer: I am the author of PatchViewer.)
You can view a diff file (a patch generated by diff tool) using Kompare from KDE environment. I used it before on Linux, but today I found how to install it on Windows. Here is an installation instruction from a blog "Kompare - the only valuable diff for M$Windows":
Go to gnuwin32 diffutils, download and install.
Download kdewin installer and launch it.
After installing kde4win - start kompare and in "Diff" section show him where your diff.exe (from gnuwin32 diffutils) is located.
Restart Kompare and it's ready to use!
You can also add an association with .diff file format and now you can view any diff files. For me it works great.
I like to use KDiff3, it's packed with functions, very user friendly and available for all popular platforms. It can also integrate with TortoiseSVN.
GitExtensions, my favorite Git GUI, also has an option to "View patch file":

A visual patch tool for Linux

I've got a file and a patch for it. I'd like to visually apply the patch, t.i. see how the changes proposed by the patch look in context, make some corrections, and save the resulting file.
What tool can do that?
Neither of the visual diff tools (i.e. meld, diffuse, diffmerge) do what I want: they don't work with patches, they merely merge whole files.
I really like Kompare. It is just a (very nice) graphical interface for diff.
http://www.caffeinated.me.uk/kompare/
sudo apt-get install kompare
Creating and applying patches
Kompare is able to create a patch file
which lists only the differences
between two compared text files A and
B. Further, Kompare can apply a patch
file which was created this way to an
original file A and, in this manner,
recompute the contents of the
corresponding file B. This is a
comfortable utility for passing a
corrected version of a file to a
friend who already has an older
version of the same file, because only
the (relatively small) patch file has
to be delivered and the receiver can
generate the corrected file by
applying the patch to the original
file.
The patches created and applied by
Kompare are compatible to patch files
generated or applied by the command
line interface diff utility, because
Kompare is in fact merely a graphical
front end to diff and the patches are
created and applied by patch, which
gets called by Kompare.
See use vimdiff with a diff file.
gvim original.file +'vert diffpa the.patch'
This will open GVim and split the window, with the original on the left and the patch applied on the right. You can then add, remove, or change hunks, and save the changes. (Well, if you want to create a new.patch you'll have to run diff again, but that's not difficult.)
You may use the emacs ediff mode.
It lets you see and validate each chunk interactively.
For what it's worth, I have this handy script in my ~/bin directory:
#!/bin/bash
if [ "$1" == "--text" ] ; then shift; fi
if diff --brief $1 $2 ; then
exit 0
else
emacs -fn 8x13bold --eval '(ediff-files "'$1'" "'$2'")'
fi
Another option: if you use Eclipse, you can generate and apply patches visually.
The idea with visual diff tools is that you can:
make a backup copy of the patched file (or a new pristine checkout of the whole tree)
apply the patch
use the visual diff tool to review the changes in context
make any desired change to the patched file within the visual diff tool.
Some tools, such as meld or diffuse will automatically diff against the previous committed version of the files.
The key insight is that you CAN apply the patch, they discard everything you don't like as long as you have a backup copy, or if you are working on a clean checkout.
If you feel more comfortable with reading and modifying unified diffs, and just want to have more context for the diff, emacs has a fairly unique feature, which is next-error-follow-mode while viewing a diff file (diff major mode). That shows the context of a diff line in the target file.
On platforms where Kompare (a fairly nice piece of software if KDE is your cup of tea), I agree that Eclipse is a great option.
I have consistently been a reluctant Eclipse user AND I am consistently impressed by how well the tools work (once the platform finally starts up).
On Fedora, I've got just about every front end installed for each of the main version control engines (SVN - also serving my Windows machines, Mercurial, git, etc.);
Meld is quite nice (no patch interface though).
Submerge works well also.
But since Eclipse does know how to apply patches and Working Copy project created itself once I figured out to create the project in the folder containing my working copy, it may become my favorite SVN front end.
I normally use GitExtensions (.NET and Mono) which also supports applying patches interactively. I hope you are already familiar with git.
Edit: Everything mentioned below this line doesn't seem to be available directly in KDiff3. And as I mentioned before: I haven't tried it yet.
GitExtension normally uses KDiff3 as diff tool, which is able to view patches.
http://kdiff3.sourceforge.net/doc/kpart.html
Using it for patches it has only a two window view, (edit) and doesn't support merging :( but it also supports 3 way diff on complete folders etc.
Actually I never tryed it with patches.
Does xxdiff do what you are looking for?
I'd use meld.
Create two copies of the files, one without and another with the patch. Use meld to compare them, and you can see exactly what the patch is changing and make changes as necessary.
Seriously, why is this so hard?

Resources