<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-6250942796758597831</atom:id><lastBuildDate>Mon, 02 Sep 2024 02:46:16 +0000</lastBuildDate><title>DayDreams...</title><description>We grow great by dreams.</description><link>http://plmday.blogspot.com/</link><managingEditor>noreply@blogger.com (Anonymous)</managingEditor><generator>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-6250942796758597831.post-3356992872720516741</guid><pubDate>Sun, 21 Apr 2013 12:33:00 +0000</pubDate><atom:updated>2013-05-07T09:41:43.537-07:00</atom:updated><title>Live diff-mode Vim editting</title><description>Recently I felt the need of an editor feature that allows me to see while eiditing a file the content differences (in highlight) between the current and the original version. On one hand, the instant feedback keeps me informed of what has changed so far, saving me from any manul invocation of other tools (internal or external) whenever I want to see the updated differences. On the other hand, it could also keep audiences informed in a presentation involving live coding. I call this feature &lt;em&gt;live &lt;code&gt;diff&lt;/code&gt;-mode editing&lt;/em&gt;. It is unfortunately missing in most editors (or IDEs) that I am aware of. But where there is imagination, there is a way. With some hacking, I managed to have this feature in Vim. &lt;sup&gt;&lt;a class=&quot;footnoteRef&quot; href=&quot;http://www.blogger.com/blogger.g?blogID=6250942796758597831#fn1&quot; id=&quot;fnref1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;br /&gt;
&lt;br /&gt;
Vim has already offered &lt;code&gt;diff&lt;/code&gt;-mode editing. It can be started either by running &lt;a href=&quot;http://vimdoc.sourceforge.net/htmldoc/diff.html&quot;&gt;&lt;code&gt;vimdiff file1 file2&lt;/code&gt;&lt;/a&gt; from the shell or by calling &lt;code&gt;:diffsplit file2&lt;/code&gt; while editing &lt;code&gt;file1&lt;/code&gt; inside Vim. This will display the contents of &lt;code&gt;file1&lt;/code&gt; and &lt;code&gt;file2&lt;/code&gt; in two windows (the former way vertically, the latter horizontally). However, neither way shows live differences. One has to always run the command &lt;code&gt;:diffupdate&lt;/code&gt; to see the updated differences after any editing. Even worse, both cannot show differences of the same file, that is, when &lt;code&gt;file1&lt;/code&gt; and &lt;code&gt;file2&lt;/code&gt; are the same.&lt;br /&gt;
&lt;br /&gt;
The second problem can be easily worked around by making a temprorary copy of the file and then start &lt;code&gt;diff&lt;/code&gt;-mode editing on them. I have the following shell script called &lt;code&gt;diffvim&lt;/code&gt; to do this automatically.&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;#!/bin/sh

# diffvim - Differentially Viming

TMPDIR=/tmp/diffvim
FILENM=$1
FILEBN=$(basename $FILENM)

if [ ! -d &quot;$TMPDIR&quot; ]; then
  mkdir $TMPDIR
fi

cp $FILENM $TMPDIR/$FILEBN &amp;amp;&amp;amp; vimdiff $FILENM $TMPDIR/$FILEBN&lt;/code&gt;&lt;/pre&gt;
It starts with making a temporary copy of the file (the filename passed as an argument to &lt;code&gt;diffvim&lt;/code&gt; in the shell) in the directory &lt;code&gt;/tmp/diffvim&lt;/code&gt; (hence all temporary copies created this way will be cleared out in between a shutdown and next reboot of the system), only if it succeeds, then it calls &lt;code&gt;vimdiff&lt;/code&gt; on the original file and the temporary copy.&lt;br /&gt;
&lt;br /&gt;
The first problem that Vim in &lt;code&gt;diff&lt;/code&gt;-mode does not update instantly the differences of the files under editing is trickier. Actually, Vim can already update one kind of differences instantly. When inserting new lines or deleting old lines, the differences are shown immediately without delay. But &lt;em&gt;inline&lt;/em&gt; editing could not trigger the update of differences. Unaware of Vim&#39;s event model, my first attempt was to configure Vim with my limited knowledge to approximate the needed behavior. The best approximation I could imagine was when leaving &lt;em&gt;INSERT&lt;/em&gt; mode and entering &lt;em&gt;NORMAL&lt;/em&gt; mode, trigger the update of differences. The command to leave the &lt;em&gt;INSERT&lt;/em&gt; mode and enter &lt;em&gt;NORMAL&lt;/em&gt; mode in Vim is associated with the &lt;kbd&gt;Esc&lt;/kbd&gt; key. So I remapped it as follows:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;imap &amp;lt;Esc&amp;gt; &amp;lt;Esc&amp;gt;:diffupdate&amp;lt;CR&amp;gt;&lt;/code&gt;&lt;/pre&gt;
This line tells Vim that, in &lt;em&gt;INSERT&lt;/em&gt; mode, remap the command associated with the &lt;kbd&gt;Esc&lt;/kbd&gt; key to &lt;code&gt;&amp;lt;Esc&amp;gt;:diffupdate&amp;lt;CR&amp;gt;&lt;/code&gt;. So when the &lt;kbd&gt;Esc&lt;/kbd&gt; key is pressed in &lt;em&gt;INSERT&lt;/em&gt; mode after any editing, it will first leave &lt;em&gt;INSERT&lt;/em&gt; mode and enter &lt;em&gt;NORMAL&lt;/em&gt; mode as usual, but this time it will also update the differences of the files under eiditing. This sounds a reasonable solution. Indeed the effect is not bad. Although the updated differences could still not be shown instantly during the editing, it can be shown immediately after the editing is done. However, it only solves a half or less of the problem. Because I soon noticed that if I modify the text directly in &lt;em&gt;NORMAL&lt;/em&gt; mode, that is, without a detour of first entering and then leaving &lt;em&gt;INSERT&lt;/em&gt; mode, the differences would still not be updated. That is sad since a central philosophy of Vim is to manipulate text as much as possible in &lt;em&gt;NORMAL&lt;/em&gt; mode. So I added another remap of the command associated with the &lt;kbd&gt;Esc&lt;/kbd&gt; key, but this time for &lt;em&gt;NORMAL&lt;/em&gt; mode:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;nmap &amp;lt;Esc&amp;gt; :diffupdate&amp;lt;CR&amp;gt;&lt;/code&gt;&lt;/pre&gt;
The expected effect was, in &lt;em&gt;NORMAL&lt;/em&gt; mode I could trigger the update of the differences by simply pressing the &lt;kbd&gt;Esc&lt;/kbd&gt; key after any direct manipulation of the text. In a sense, it worked. But it also brought in some unexpected effects: if after running &lt;code&gt;diffvim file&lt;/code&gt; from the shell (thus starting my &lt;em&gt;approximate&lt;/em&gt; live &lt;code&gt;diff&lt;/code&gt;-mode editing), the first thing I do was not pressing the &lt;kbd&gt;Esc&lt;/kbd&gt; key but issuing some other &lt;em&gt;NORMAL&lt;/em&gt; mode commands, like pressing the key &lt;kbd&gt;j&lt;/kbd&gt; or &lt;kbd&gt;l&lt;/kbd&gt; to move the cursor around, I was put into &lt;em&gt;INSERT&lt;/em&gt; mode. I tried hard to fix this problem but all my attempts failed. Eventually, I turned to the &lt;a href=&quot;http://unix.stackexchange.com/questions/72205/live-diff-mode-editing-in-vim&quot;&gt;Unix StackExchange&lt;/a&gt; for help. There I learned that overloading the &lt;kbd&gt;Esc&lt;/kbd&gt; is a bad idea and that the proper way to achieve what I want is to let Vim execute the &lt;code&gt;:diffupdate&lt;/code&gt; command automatically in an event-driven way. The event names suggested there, &lt;code&gt;InsertEnter&lt;/code&gt; and &lt;code&gt;InsertLeave&lt;/code&gt;, only covers respectively the events of &lt;em&gt;Entering&lt;/em&gt; and &lt;em&gt;Leaving&lt;/em&gt; &lt;em&gt;INSERT&lt;/em&gt; mode. So associating &lt;code&gt;:diffupdate&lt;/code&gt; to them would only update the differences when entering or leaving the &lt;em&gt;INSERT&lt;/em&gt; mode, which my ad-hoc solution could do as well. However, it did suggest the right way to a thorough solution. After checking the supported events in Vim&#39;s help, I found the right event names to capture any text change in both &lt;em&gt;INSERT&lt;/em&gt; and &lt;em&gt;NORMAL&lt;/em&gt; mode. The events &lt;code&gt;CursorMoved&lt;/code&gt; or &lt;code&gt;CursorMovedI&lt;/code&gt; are triggered whenever the cursor is moved in &lt;em&gt;NORMAL&lt;/em&gt; or &lt;em&gt;INSERT&lt;/em&gt; mode. The following command associates the command &lt;code&gt;:diffupdate&lt;/code&gt; to these two events:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;autocmd CursorMoved,CursorMovedI * :diffupdate&lt;/code&gt;&lt;/pre&gt;
It tells Vim to update the differences of whatever file (since its name always matches the wildcard filename pattern &lt;code&gt;*&lt;/code&gt;) under editing whenever the events &lt;code&gt;CursorMoved&lt;/code&gt; or &lt;code&gt;CursorMovedI&lt;/code&gt; is triggered. To see why these events do the job perfectly, one only needs to notice that any inline editing, whether in &lt;em&gt;INSERT&lt;/em&gt; or &lt;em&gt;NORMAL&lt;/em&gt; mode, will involve moving the cursor either forward or backward.&lt;br /&gt;
&lt;br /&gt;
Later, I also learned that it is better to restrict specific settings for Vim in &lt;code&gt;diff&lt;/code&gt; mode local by collecting them into the following conditional structure:&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;if &amp;amp;diff
  autocmd CursorMoved,CursorMovedI * :diffupdate
endif&lt;/code&gt;&lt;/pre&gt;
I could not find an explanation of &lt;code&gt;&amp;amp;diff&lt;/code&gt; in the Vim help. But clearly it is a boolean variable set when Vim is in &lt;code&gt;diff&lt;/code&gt; mode.&lt;br /&gt;
&lt;br /&gt;
The only drawback I can see of this solution is that, in &lt;em&gt;NORMAL&lt;/em&gt; mode, just moving around without changing the text will also trigger the update of the differences even though there is no difference to udpate. But I view this as a trade-off.&lt;br /&gt;
&lt;br /&gt;
That is all to have this interesting feature. Enjoy live &lt;code&gt;diff&lt;/code&gt;-mode editing in Vim!&lt;br /&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;hr /&gt;
&lt;ol&gt;
&lt;li id=&quot;fn1&quot;&gt;Emacs allows the user to see the differences whenever she wants by invoking the command &lt;a href=&quot;http://www.gnu.org/software/emacs/manual/html_node/emacs/Comparing-Files.html&quot;&gt;&lt;code&gt;diff-buffer-with-file&lt;/code&gt;&lt;/a&gt;, which is already quite close to what I want. But it is still &lt;em&gt;not&lt;/em&gt; live. However, given Emacs&#39;s great configurability, it should not be difficult to hack out a way to do the same thing.&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=6250942796758597831#fnref1&quot;&gt;↩&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</description><link>http://plmday.blogspot.com/2013/04/recently-i-felt-need-of-editor-feature.html</link><author>noreply@blogger.com (Anonymous)</author><thr:total>0</thr:total></item></channel></rss>