Kdiff 3
Kdiff 3
2
Contents
1 Introduction 7
1.1 Yet Another Diff Frontend? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.2 Screenshots and Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.3 More Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3.1 Line-By-Line And Char-By-Char Diff-Viewer . . . . . . . . . . . . . . . . . 9
1.3.2 See White-Space Differences At One Glance . . . . . . . . . . . . . . . . . . 9
1.3.3 Triple-Diff . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.3.4 Comfortable Merge Of Two Or Three Input Files . . . . . . . . . . . . . . . 10
1.3.5 And ... . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
2.5.2 Setting The Current Group And Synchronising Merge And Diff View Position 16
2.5.3 Choosing Inputs A, B or C For Current Conflict And Editing . . . . . . . . . 17
2.5.4 Choosing Input A, B, or C for All Conflicts . . . . . . . . . . . . . . . . . . . 17
2.5.5 Automatic Merge of Version Control Keywords and History (Log) . . . . . 17
2.6 Navigation And Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
2.6.1 Auto-Advance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.7 Select, Copy And Paste . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.8 Saving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
2.9 Finding Strings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.10 Printing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.11 Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.11.1 Font . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.11.2 Colors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.11.3 Editor Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.11.4 Diff Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.11.5 Merge Settings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.11.6 Folder Merge . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.11.7 Regional and Language Options . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.11.8 Miscellaneous . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.11.9 Configuring Keyboard-Shortcuts . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.12 Preprocessor Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.12.1 sed Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.12.2 Examples For sed Use In KDiff3 . . . . . . . . . . . . . . . . . . . . . . . . . 28
[Link] Ignoring Other Types Of Comments . . . . . . . . . . . . . . . . . 28
[Link] Caseinsensitive Diff . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
[Link] Ignoring Version Control Keywords . . . . . . . . . . . . . . . . . . 29
[Link] Ignoring Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
[Link] Ignoring Certain Columns . . . . . . . . . . . . . . . . . . . . . . . 29
[Link] Combining Several Substitutions . . . . . . . . . . . . . . . . . . . 29
[Link] Using perl instead of sed . . . . . . . . . . . . . . . . . . . . . . . . 30
2.12.3 Order Of Preprocessor Execution . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.12.4 Warning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
4
The KDiff3 Handbook
4 Miscellaneous Topics 40
4.1 Network transparency via KIO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.1.1 KIO-Slaves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.1.2 How To Write URLs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
4.1.3 Capabilities of KIO-Slaves . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.2 Using KDiff3 as a Git Diff and Merging Tool . . . . . . . . . . . . . . . . . . . . . . 41
5
Abstract
Chapter 1
Introduction
7
The KDiff3 Handbook
3-way-merging is fully supported. This is useful if two people change code independently. The
original file (the base) is used to help KDiff3 to automatically select the correct changes. The
merge-editor below the diff-windows allows you to solve conflicts, while showing you the output
you will get. You can even edit the output. This screenshot shows three input files being merged:
KDiff3 also helps you to compare and merge complete folders. This screenshot shows KDiff3
during a folder merge:
8
The KDiff3 Handbook
1.3.3 Triple-Diff
Analyze three files and see where they differ.
The left/middle/right windows are named A/B/C and have the blue/green/magenta color re-
spectively.
9
The KDiff3 Handbook
If one file is the same and one file is different on a line then the color shows which file is different.
The red color means that both other files are different.
KDiff3 can be used to merge two or three input files and automatically merges as much as pos-
sible. The result is presented in an editable window where most conflicts can be solved with a
single mouseclick: Select the respective ... buttons at the A/ B/C windows to select the source
that should be used. You can also select more than one source. Since this output window is an
editor even conflicts which need further corrections can be solved here without requiring another
tool.
10
The KDiff3 Handbook
Chapter 2
If all files have the same name but are in different folders, you can reduce typework by specifying
the filename only for the first file, e.g.:
kdiff3 folder1 / filename folder2 folder3
11
The KDiff3 Handbook
For folder comparison and merge you can continue to read here.
Example output:
Options :
-m , -- merge Merge the input .
-b , -- base file Explicit base file . For compatibility with ←-
certain tools .
-o , -- output file Output file . Implies -m. E.g .: -o newfile . txt
-- out file Output file , again . ( For compatibility with ←-
certain tools .)
-- noauto Ignore -- auto and always show GUI .
-- auto No GUI if all conflicts are auto - solvable . ( ←-
Needs -o file )
--L1 alias1 Visible name replacement for input file 1 ( base ←-
).
--L2 alias2 Visible name replacement for input file 2.
--L3 alias3 Visible name replacement for input file 3.
-L , -- fname alias Alternative visible name replacement . Supply ←-
this once for every input .
--cs string Override a config setting . Use once for every ←-
setting . E.g .: --cs " AutoAdvance =1"
-- confighelp Show list of config settings and current values ←-
.
-- config file Use a different config file .
The option --cs allows you to adjust a configuration value that is otherwise only adjustable via
the configure dialogs. But be aware that when KDiff3 then terminates the changed value will
be stored along with the other settings. With --confighelp you can find out the names of the
available items and current values.
Via --config you can specify a different config file. When you often use KDiff3 with completely
different setups this allows you to easily switch between them.
Many people want to use KDiff3 with some version control system. But when that version control
system calls KDiff3 using command line parameters that KDiff3 doesn’t recognise, then KDiff3
terminates with an error. The Integration item in the settings dialog allow to specify command
line parameters that should be ignored by KDiff3. They will appear in the usage help like in this
example:
12
The KDiff3 Handbook
When this isn’t enough, then it is recommended to write a shell script that does the option trans-
lation.
2.2 Open-Dialog
Since many input files must be selectable, the program has a special open dialog:
The open dialog allows you to edit the filenames by hand, selecting a file via the file-browser via
the File... button or allows you to choose recent files from the drop-down lists. If you open the
dialog again, then the current names still remain there. The third input file is not required. If the
entry for C remains empty, then only a two file diff analysis will be done.
You can also select a folder via the Folder... button. If for A a folder is specified then a folder-
comparison /merge starts. If A specifies a file but B, C or the output specify a folder, then KDiff3
uses the filename from A in the specified folders.
If Merge check box is selected, then the Output line becomes editable. But it is not required to
specify the output filename immediately. You can also postpone this until saving.
The Configure... button opens the settings dialog, so that you can set the options before running
the analysis.
13
The KDiff3 Handbook
N OTE
Currently you cannot drag anything from KDiff3. Only dropping in the diff input is supported.
WARNING
Some editors still interpret the drag and drop into another program like cut (instead of copy) and paste.
Your original data might be lost then.
2.4.2 Coloring
The three input windows are assigned the letters A, B and C. A has color blue, B has green and C
has magenta. (These are the defaults, but can be changed in the Color item in the settings dialog.)
When a difference is detected then the color shows which input file differs. When both other
input files differ then the color used to express this is red by default (Conflict color option in the
Color item in the settings dialog). This colorscheme is especially useful in the case of three input
files, which will be seen in the next section (Merging).
14
The KDiff3 Handbook
Left of each text is the ˝summary˝ column. If differences occurred on a line then the summary
column shows the respective color. For a white-space-only difference the summary is chequered.
For programming languages where white space is not so important this is useful to see at one
glance if anything of importance was modified. (In C/C++ white space is only interesting within
strings, comments, for the preprocessor, and some only very esoteric situations.)
The vertical line separating the summary column and the text is interrupted if the input file had
no lines there. When word-wrap is enabled then this vertical line appears dotted for wrapped
lines.
Sometimes the algorithm places the wrong lines next to each other. Or you want to compare
one piece of text with text at a completely different position in the other file. For these situations
you can manually instruct KDiff3 to align certain lines. Mark the text for which you want to
improve the alignment with the mouse as you would for copy and paste in the first diff view
and then choose Diffview → Add Manual Diff Alignment menu item (Ctrl-Y). An orange bar
will appear in the summary column next to the chosen text. Repeat this for the second and (if
available) third diff view. KDiff3 will immediately recalculate the differences everytime you do
this, and will align the chosen lines. Of course some of the previously matching lines in between
might not match anymore.
Currently merging doesn’t support the use of manual diff help.
In some cases KDiff3 will see too many or too few diff sections for merging. In such a case you
can join or split existing sections.
Add new sections by first selecting text in the lines that belong together in either input window
(as for copying to the clipboard). Then choose Merge → Split Diff At Selection menu item. Splits
will be added above the first line and below the last line of the selected text. If you only want to
add one section, then select text beginning at another section-split.
For joining sections in either input window select something in the lines from the sections to join.
(You can join several sections in one step too.) Then choose Merge → Join Selected Diffs menu
item.
15
The KDiff3 Handbook
The merge output editor window (below the diff input windows) also has an info line at the
top showing Output:, the filename and [Modified] tag if you edited something. Usually it will
contain some text through the automatic merge facilities, but often it will also contain conflicts.
!!! Saving is disabled until all conflicts are resolved !!! (Use the Go to Previous/Next Unsolved
Conflict buttons to find the remaining conflicts.)
With only two input files every difference is also a conflict that must be solved manually.
With three input files the first file is treated as base, while the second and third input files contain
modifications. When at any line only either input B or input C have changed but not both then
the changed source will automatically be selected. Only when B and C have changed on the same
lines, then the tool detects a conflict that must be solved manually. When B and C are the same,
but not the same as A, then C is selected.
2.5.2 Setting The Current Group And Synchronising Merge And Diff View
Position
When clicking into the summary column with the left mouse button in either window then the be-
ginning of the group belonging to that line will shown in all windows. This group then becomes
the ˝current group˝. It is highlighted with the Current range diff background color option of
Integration item in the settings dialog and a black bar appears on the left side of the text.
16
The KDiff3 Handbook
N OTE
When choosing either source for unsolved white space conflicts and the Ignore numbers (treat as
white space) or Ignore C/C++ comments (treat as white space) options at Diff item in the settings
dialog are used then changes in numbers or comments will be treated like white space too.
17
The KDiff3 Handbook
version control history on merge start at Merge item in the settings dialog or later via the Merge
→ Automatically Solve History Conflicts menu item.
Usually the version control history begins with a line containing the keyword ˝$Log$˝. This must
be matched by the History start regular expression: option. KDiff3 detects which subsequent
lines are in the history by analysing the leading characters that came before the ˝$Log$˝ keyword.
If the same ˝leading comment˝ characters also appears in the following lines, then they are also
included in the history.
During each check-in the VCS writes a unique line specifying version-, date- and time-
information followed by lines with user comments. These lines form one history-entry. This
history section grows with every check-in and the most recent entries appear at the top (after the
history start line).
When for parallel development two or more developers check-in a branch of the file then the
merge history will contain several entries that appear as conflicts during the merge of the
branches. Since merging these can become very tedious, KDiff3 offers support with two pos-
sible strategies: Just insert the history information from both contributors at the top or sort the
history information by a user defined key.
The just-insert-all-entries-method is easier to configure. KDiff3 just needs a method to detect,
which lines belong to one history entry. Most VCS insert an empty line after each history entry. If
there are no other empty lines, this is a sufficient criterion for KDiff3. Just set an empty History
entry start regular expression at Merge item in the settings dialog. If the empty line criterion
isn’t sufficient, you can specify a regular expression to detect the history entry start.
Note that KDiff3 will remove duplicate history entries. If a history entry appeared several times
in the history of a input file, only one entry will remain in the output.
If you want to sort the history, then you have to specify how the sort key should be built. Use
parentheses in the History entry start regular expression at Merge item in the settings dialog
to group parts of the regular expression that should later be used for the sort key. Then specify
the History entry start sort key order option specifying a comma ˝,˝ separated list of numbers
referring to the position of the group in the regular expression.
Because this is not so easy to get right immediately, you are able to test and improve the regular
expressions and key-generation in a dedicated dialog by pressing the Test your regular expres-
sions button.
Example: Assume a history that looks like this:
/**************************************************************************
** HISTORY : $Log : \ toms_merge_main_view \ MyApplication \ src \ ←-
complexalgorithm . cpp $
**
** \ main \ integration_branch_12 2 Apr 2001 10:45:41 tom
** Merged branch simon_branch_15 .
**
** \ main \ henry_bugfix_branch_7 \1 30 Mar 2001 19:22:05 henry
** Improved the speed for subroutine convertToMesh () .
** Fixed crash .
**************************************************************************/
The history start line matches the regular expression ˝.*\$Log.*\$.*˝. Then follow the history
entries.
The line with the ˝$Log$˝ keyword begins with two ˝*˝ after which follows a space. KDiff3 uses
the first non-white-space string as ˝leading comment˝ and assumes that the history ends in the
first line without this leading comment. In this example the last line ends with a string that also
starts with two ˝*˝, but instead of a space character more ˝*˝ follow. Hence this line ends the
history.
If history sorting isn’t required then the history entry start line regular expression could look like
this. (This line is split in two because it wouldn’t fit otherwise.)
18
The KDiff3 Handbook
\s *\\ main \\\ S +\ s +[0 -9]+ ( Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | Dec )
[0 -9][0 -9][0 -9][0 -9] [0 -9][0 -9]:[0 -9][0 -9]:[0 -9][0 -9]\ s +.*
For details about regular expressions please see the regular expression documentation. Note that
˝\s˝ (with lowercase ˝s˝) matches any white space and ˝\S˝ (with uppercase ˝S˝) matches any
non-white-space. In our example the history entry start contains first the version info with reg.
exp. ˝\\main\\\S+˝, the date consisting of day ˝[0-9]+˝, month ˝(Jan|Feb|Mar|Apr|May|Jun
|Jul|Aug|Sep|Oct|Nov|Dec)˝ and year ˝[0-9][0-9][0-9][0-9]˝, the time ˝[0-9][0-9]:[0-9]
[0-9]:[0-9][0-9]˝ and finally the developers login name ˝.*˝.
Note that the ˝leading comment˝ characters (in the example ˝**˝) will already be removed by
KDiff3 before trying to match, hence the regular expression begins with a match for none or more
white-space characters ˝\s*˝. Because comment characters can differ in each file (e.g. C/C++
uses other comment characters than a Perl script) KDiff3 takes care of the leading comment char-
acters and you should not specify them in the regular expression.
If you require a sorted history. Then the sortkey must be calculated. For this the relevant parts in
the regular expression must be grouped by parentheses. (The extra parentheses can also stay in
if history sorting is disabled.)
\s *\\ main \\(\ S +) \s +([0 -9]+) ( Jan | Feb | Mar | Apr | May | Jun | Jul | Aug | Sep | Oct | Nov | ←-
Dec )
([0 -9][0 -9][0 -9][0 -9]) ([0 -9][0 -9]:[0 -9][0 -9]:[0 -9][0 -9]) \s +(.*)
The parentheses now contain 1. version info, 2. day, 3. month, 4. year, 5. time, 6. name. But if we
want to sort by date and time, we need to construct a key with the elements in a different order
of appearance: First the year, followed by month, day, time, version info and name. Hence the
sortkey order to specify is ˝4,3,2,5,1,6˝.
Because month names aren’t good for sorting (˝Apr˝ would be first) KDiff3 detects in which
order the month names were given and uses that number instead (˝Apr˝ -> ˝04˝). And if a pure
number is found it will be transformed to a 4-digit value with leading zeros for sorting. Finally
the resulting sort key for the first history entry start line will be:
2001 04 0002 10:45:41 integration_branch_12 tom
19
The KDiff3 Handbook
2.6.1 Auto-Advance
There also is a button Automatically Go to Next Unsolved Conflict After Source Selection
(Auto-Advance). If you enable this, then, when one source is selected, KDiff3 will jump to and
select the next unsolved conflict automatically. This can help when you always want to choose
one source only. When you need both sources, or you want to edit after selecting, then you prob-
ably want to switch this off. Before proceeding to the next unsolved conflict KDiff3 shows you
the effect of your choice for a short time. This delay is adjustable in the Merge item in the settings
dialog: You can specify the Auto advance delay (ms): in milli seconds between 0 and 2000. Hint:
Tired of many clicks? - Use a small Auto-Advance-delay and the shortcuts Ctrl-1/2/3 to select
A/B/C for many conflicts.
2.8 Saving
Saving will only be allowed, when all conflicts were solved. If the file already exists and the
Backup files option is enabled at Folder item in the settings dialog then the existing file will be
renamed with an .orig extension, but if such a file exists it will be deleted. When you exit or start
another diff-analysis and data wasn’t saved yet, then KDiff3 will ask if you want to save, cancel
or proceed without saving. (KDiff3 does not catch any signals. So if you ˝kill˝ a KDiff3 instance
then your data will be lost.)
Line endings are saved according to the normal method on the underlying operating system. For
Unices each line ends with an linefeed-character ˝\n˝, while for Win32-based systems each line
ends with a carriage-return + a linefeed ˝\r\n˝. KDiff3 does not preserve the line-endings of the
input files, which also means that you shouldn’t use KDiff3 with binary files.
20
The KDiff3 Handbook
2.10 Printing
KDiff3 supports printing for textfile differences. The File → Print... menu item (Ctrl-P) opens a
dialog that allows you to select the printer and to adjust other options.
There are several possibilities to adjust the range. Due to different printing dialogs on different
operating systems, the method to achieve certain range selections varies.
All:
Print everything.
Current:
Print a page starting at the first visible line in the window. (On systems without this option
this can be achieved by specifying page number 10000 for printing.)
Selection:
Before choosing to print select text with the mouse (like for copy and paste) in one of the diff
input windows to define the start and end line. If no text in one of the diff input windows
was selected, then this won’t be an available choice. (On systems without this option this
can be achived by specifying page number 9999 for printing.)
Range:
Specify the first and last page.
Other important options for printing will be taken from the normal options:
2.11 Options
Options and the recent-file-list will be saved when you exit the program, and reloaded when you
start it. (Settings → Configure KDiff3... menu item).
21
The KDiff3 Handbook
2.11.1 Font
Select a fixed width font. (On some systems this dialog will also present variable width fonts, but
you should not use them.)
2.11.2 Colors
Foreground color:
Usually black.
Background color:
Usually white.
Diff background color:
Usually light gray.
Color A:
Usually dark blue.
Color B:
Usually dark green.
Color C:
Usually dark magenta.
Conflict color:
Usually red.
Current range background color:
Usually light yellow.
Current range diff background color:
Usually dark yellow.
Color for manually aligned difference ranges:
Usually orange.
Folder Comparison View:
Changing the colors for folder comparison will be effective only when starting the next folder
comparison.
On systems with only 16 or 256 colors some colors are not available in pure form. On such
systems the Defaults button will choose a pure color.
22
The KDiff3 Handbook
When comparing files, KDiff3 first it tries to match lines that are equal in all input files. Only
during this step it might ignore white space. The second step compares each line. In this step
white space will not be ignored. Also during the merge white space will not be ignored.
23
The KDiff3 Handbook
24
The KDiff3 Handbook
These options are concerned with scanning the folder and handling the merge: See the Folder
Comparison/Merge Docs for details.
Yet there is one option here that is also relevant for saving single files:
25
The KDiff3 Handbook
N OTE
This documentation was written assuming that this option or reverse layout are disabled. So
some references to ˝left˝ or ˝right˝ must be replaced by their respective counterpart if you use
these options.
2.11.8 Miscellaneous
(These options and actions are available in menus or the buttonbar.)
Overview options:
These choices are only available when you compare three files. In normal mode all differ-
ences are shown in one color-coded overview-column. But sometimes you are especially
interested in the differences between only two of these three files. Selecting ˝A vs. B˝,
˝A vs. C˝ or ˝B vs. C˝-overview will show a second overview column with the required
information next to the normal overview.
Word Wrap Diff Windows
Wrap lines when their length would exceed the width of a window.
Show Window A/B/C:
Sometimes you want to use the space on the screen better for long lines. Hide the windows
that are not important. (In the Windows-menu.)
Toggle Split Orientation
Switch between diff windows shown next to each other (A left of B left of C) or above each
other (A above B above C). This should also help for long lines. (In the Window menu.)
Merge Current File
Works if you only compare two files. A single click starts the merge and uses the filename
of the last input-file as the default output filename. (When this is used to restart a merge,
then the output filename will be preserved.)
Show White Space
Turn this off to suppress any highlighting of white-space-only changes in the text or
overview-columns. (Note that this also applies to changes in numbers or comments if the
options Ignore numbers (treat as white space) or Ignore C/C++ comments (treat as white
space) are active.)
Show Space && Tabulator Characters
Sometimes the visible spaces and tabs are disturbing. You can turn this off.
Show Line Numbers
You can select if line numbers should be shown for the input files.
26
The KDiff3 Handbook
Preprocessor command:
When any file is read, it will be piped through this external command. The output of this
command will be visible instead of the original file. You can write your own preprocessor
that fulfills your specific needs. Use this to cut away disturbing parts of the file, or to
automatically correct the indentation etc.
Line-matching preprocessor command:
When any file is read, it will be piped through this external command. If a preprocessor-
command (see above) is also specified, then the output of the preprocessor is the input of
the line-matching preprocessor. The output will only be used during the line matching
phase of the analysis. You can write your own preprocessor that fulfills your specific needs.
Each input line must have a corresponding output line.
The idea is to allow the user greater flexibility while configuring the diff-result. But this requires
an external program, and many users don’t want to write one themselves. The good news is that
very often sed or perl will do the job.
Example: Simple testcase: Consider file [Link] (6 lines):
aa
ba
ca
da
ea
fa
Without a preprocessor the following lines would be placed next to each other:
aa - cg
ba - dg
ca - eg
da
ea
fa
This is probably not wanted since the first letter contains the actually interesting information. To
help the matching algorithm to ignore the second letter we can use a line matching preprocessor
command, that replaces ’g’ with ’a’:
sed ’s/g/a/’
27
The KDiff3 Handbook
Internally the matching algorithm sees the files after running the line matching preprocessor, but
on the screen the file is unchanged. (The normal preprocessor would change the data also on the
screen.)
Before you use a new command within KDiff3, you should first test it in a console. Here the echo
command is useful. Example:
echo abrakadabra | sed ’s/a/o/’
-> obrakadabra
This example shows a very simple sed-command that replaces the first occurrence of ˝a˝ with
˝o˝. If you want to replace all occurrences then you need the ˝g˝ flag:
echo abrakadabra | sed ’s/a/o/g ’
-> obrokodobro
The ˝|˝-symbol is the pipe-command that transfers the output of the previous command to the
input of the following command. If you want to test with a longer file then you can use cat on
UNIX® like systems or type on Windows® like systems. sed will do the substitution for each line.
cat filename | sed options
Currently KDiff3 understands only C/C++ comments. Using the Line-matching preprocessor
command: option you can also ignore other types of comments, by converting them into C/C++-
comments.
Example: To ignore comments starting with ˝#˝, you would like to convert them to ˝/ / ˝. Note
that you also must enable the Ignore C/C++ comments (treat as white space) option to get an
effect. An appropriate Line-matching preprocessor command: would be:
sed ’s /#/\/\// ’
Since for sed the ˝/ ˝ character has a special meaning, it is necessary to place the ˝\˝ character
before each ˝/ ˝ in the replacement-string. Sometimes the ˝\˝ is required to add or remove a
special meaning of certain characters. The single quotation marks (’) are only important when
testing on the command shell as it will otherwise attempt to process some characters. KDiff3
does not do this except for the escape sequences ’\˝’ and ’\\’.
28
The KDiff3 Handbook
Use the following Line-matching preprocessor command: to convert all input to uppercase:
sed ’s /\(.*\) /\ U \1/ ’
Here the ˝.*˝ is a regular expression that matches any string and in this context matches all
characters in the line. The ˝\1˝ in the replacement string refers to the matched text within the
first pair of ˝\(˝ and ˝\)˝. The ˝\U˝ converts the inserted text to uppercase.
CVS and other version control systems use several keywords to insert automatically generated
strings (info:/cvs/Keyword substitution). All of them follow the pattern ˝$KEYWORD generated
text$˝. We now need a line-matching preprocessor command that removes only the generated
text:
sed ’s /\ $ \( Revision \| Author \| Log \| Header \| Date \) .*\ $ /\ $ \1\ $/’
The ˝\|˝ separates the possible keywords. You might want to modify this list according to your
needs. The ˝\˝ before the ˝$˝ is necessary because otherwise the ˝$˝ matches the end of the line.
While experimenting with sed you might come to understand and even like these regular expres-
sions. They are useful because there are many other programs that also support similar things.
Ignoring numbers actually is a built-in option. But as another example, this is how it would look
as a line-matching preprocessor command.
sed ’s /[0123456789. -]// g ’
Any character within ’[’ and ’]’ is a match and will be replaced with nothing.
Sometimes a text is very strictly formatted, and contains columns that you always want to ignore,
while there are other columns you want to preserve for analysis. In the following example the
first five columns (characters) are ignored, the next ten columns are preserved, then again five
columns are ignored and the rest of the line is preserved.
sed ’s /.....\(..........\) .....\(.*\) /\1\2/ ’
Each dot ’.’ matches any single character. The ˝\1˝ and ˝\2˝ in the replacement string refer to the
matched text within the first and second pair of ˝\(˝ and ˝\)˝ denoting the text to be preserved.
Sometimes you want to apply several substitutions at once. You can then use the semicolon ’;’
to separate these from each other. Example:
echo abrakadabra | sed ’s/a/o/g;s /\(.*\) /\ U \1/ ’
-> OBROKODOBRO
29
The KDiff3 Handbook
Instead of sed you might want to use something else like perl.
perl -p -e ’s/ REGEXP / REPLACEMENT / FLAGS ’
But some details are different in perl. Note that where sed needed ˝\(˝ and ˝\)˝ perl requires
the simpler ˝(˝ and ˝)˝ without preceding ’\’. Example:
sed ’s /\(.*\) /\ U \1/ ’
perl -p -e ’s /(.*) /\ U \1/ ’
The data is piped through all internal and external preprocessors in the following order:
• Normal preprocessor,
• Line-matching preprocessor,
• Ignore case (treat as white space) (conversion to uppercase),
• Detection of C/C++ comments,
• Ignore numbers (treat as white space),
• Ignore white space
The data after the normal preprocessor will be preserved for display and merging. The other
operations only modify the data that the line-matching-diff-algorithm sees.
In the rare cases where you use a normal preprocessor note that the line-matching-preprocessor
sees the output of the normal preprocessor as input.
2.12.4 Warning
The preprocessor-commands are often very useful, but as with any option that modifies your
texts or hides away certain differences automatically, you might accidentally overlook certain
differences and in the worst case destroy important data.
For this reason during a merge if a normal preprocessor-command is being used KDiff3 will tell
you so and ask you if it should be disabled or not. But it won’t warn you if a Line-matching
preprocessor command: option is active. The merge will not complete until all conflicts are
solved. If you disabled Diffview → Show White Space menu item then the differences that were
removed with the Line-matching preprocessor command: option will also be invisible. If the
Save button remains disabled during a merge (because of remaining conflicts), make sure to en-
able Diffview → Show White Space menu item. If you don’t want to merge these less important
differences manually you can select Merge → Choose [A|B|C] for All Unsolved Whitespace
Conflicts menu item.
30
The KDiff3 Handbook
Chapter 3
31
The KDiff3 Handbook
When three folders are merged then folder1 is used as the base for the merge. If no destination
folder is specified, then KDiff3 will use folder3 as the destination folder for the merge.
Note that only the comparison starts automatically, not the merge. For this you first must select
a menu entry or the F7 key. (More details later.)
... and details about the currently selected item on the right:
32
The KDiff3 Handbook
• Normal file
• Normal folder (folder image)
• Link to a file (file image with a link arrow)
• Link to a folder (folder image with a link arrow)
If the file type is different in the other folders, then this is visible in the columns A/B/C and in
the window that shows the details about the selected item. Note that for such a case no merge
operation can be selected automatically. When starting the merge, then the user will be informed
of problems of that kind.
33
The KDiff3 Handbook
After comparing the folders KDiff3 also evaluates a proposal for a merge operation. This is
shown in the Operation column. You can modify the operation by clicking on the operation you
want to change. A small menu will popup and allows you to select an operation for that item.
(You can also select the most needed operations via keyboard. Ctrl-1/2/3/4/Del will select
A/B/C/Merge/ Delete respectively if available.) This operation will be executed during the
merge. It depends on the item and on the merge-mode you are in, what operations are available.
The merge-mode is one of
In three-folder merge the operation proposal will be: If for an item ...
In two-folder merge the operation proposal will be: If for an item ...
Sync-mode is active if only two folders and no explicit destination were specified and if the
Synchronize folders option is active. KDiff3 then selects a default operation so that both folders
are the same afterwards. If for an item ...
34
The KDiff3 Handbook
When two folders are merged and the Copy newer instead of merging (unsafe) option is se-
lected, then KDiff3 looks at the dates and proposes to choose the newer file. If the files are not
equal but have equal dates, then the operation will contain ˝Error: Dates are equal but files are
not.˝ While such items exist the folder merge cannot start.
Several options influence which files are listed here. Some are accessible in the settings dialog.
The Folder menu contains the entries:
• Show Identical Files: Files that have been detected equal in all input folders.
• Show Different Files: Files that exist in two or more folders but are not equal.
• Show Files only in A: Files that exist only in A, but not in B or C.
• Show Files only in B: Files that exist only in B, but not in A or C.
• Show Files only in C: Files that exist only in C, but not in A or B.
Activate only the Show options for the items you want listed. If for example you only want to
list all items that exist either in A or in B but not in both, you’ll have to activate Show Files only
in A and Show Files only in B and deactivate all others (Show Identical Files, Show Different
Files, Show Files only in C). The list will be updated immediately to reflect the change.
These options also apply for folders with one exception: Disabling Show Different Files will
not hide any complete folders. This will work only for files within.
Note that of these only the Show Identical Files option is persistant. The others are enabled
when starting KDiff3.
35
The KDiff3 Handbook
• Recursive Folders: If this is off, then items in subfolders will not be found.
• Pattern/Anti-Pattern: Include/exclude items that match
• Exclude Hidden Files
• Show options (Show Identical/Different Files, Files only in A/B/C)
If you change the settings in order to list more files, you must do a rescan via menu Folder
→ Rescan yourself. (The reason for this is that for faster comparison-speed KDiff3 omits the
comparison for files suppressed by these criteria.) If you changed your file and folder patterns to
exclude files, then the file-list will immediately be updated on closing the options-dialog.
Note that when you write to a completely new folder then you usually also want to copy the
identical files. In that case enable the Show Identical Files option. If your destination folder is
one of the inputs, then this isn’t necessary because the file is already there.
If you are satisfied so far, the rest is easy.
To merge all items: Select Start/Continue Folder Merge in the Folder menu or press F7 (which is
the default shortcut). To merge only the current item: Select Run Operation for Current Item or
press F6.
If due to conflicting filetypes some items with invalid operations still exist, then a messagebox
will appear and these items will be pointed out, so you can select a valid operation for the item.
If you merge all items a dialog will appear giving you the options Do it, Simulate it and Cancel.
• Select Simulate it if you want to see what would be done without actually doing it. A verbose
list of all operations will be shown.
• Otherwise select Do it to really start merging.
Then KDiff3 will run the specified operation for all items. If manual interaction is required (single
file merge), then a merge window will open (see the big screenshot).
When you have finished with manually merging a file, again select Start/ Continue Folder Merge
or the F7 key. If you haven’t saved it yet, a dialog will ask you to do so. Then KDiff3 will continue
with the next item.
When KDiff3 encounters an error, it will tell you so and will show the verbose-status-information.
At the bottom of this list, there will be some error messages which should help you to understand
the cause of the problem. When you continue merging (F7 key) KDiff3 will give you the choice to
retry or skip the item that caused the problem. This means that before continuing you can choose
another operation or solve the problem by other means.
When the merge is complete, then KDiff3 will inform you via a message box.
If some items were merged individually before running the directorymerge then KDiff3 remem-
bers this (while this merge-session goes on), and doesn’t merge them again when later the merge
for all items is run. Even when the merge was skipped or nothing was saved these items count
as completed. Only when you change the merge operation the Done status of the item will be
removed and it can be merged again.
36
The KDiff3 Handbook
Recursive folders
Select whether to search folders recursively.
File pattern(s):
Only files that match any pattern here will be put in the tree. More than one pattern may
be specified here by using the semicolon ˝;˝ as separator. Valid wildcards: ’*’ and ’?’. (e.g.
˝*.cpp;*.h˝). Default is ˝*˝. This pattern is not used on folders.
File-anti-pattern(s):
Files that match this pattern will be excluded from the tree. More than one pattern may be
specified here via using the semicolon ˝;˝ as separator. Valid wildcards: ’*’ and ’?’. Default
is ˝*.orig;*.o;*.obj˝.
Folder-anti-pattern(s):
Folders that match this pattern will be excluded from the tree. More than one pattern may
be specified here via using the semicolon ˝;˝ as separator. Valid wildcards: ’*’ and ’?’.
Default is ˝CVS;deps;.svn˝.
Use Ignore File
Ignore files and folders that would also be ignored by the your source control. Many auto-
matically generated files are ignored using ignore lists. The big advantage is that this can
be folder-specific via a local ignore file. (See version control docs for more detail.)
Find hidden files and folders
On some file systems files have an ˝Hidden˝-attribute. On other systems a filename starting
with a dot ˝.˝ causes it to be hidden. This option allows you to decide whether to include
these files in the tree or not. Default is on.
Follow file links
For links to files: When disabled, then the symbolic links are compared. When enabled,
then the files behind the links are compared. Default is off.
Follow folder links
For links to folders: When disabled, then the symbolic links will be compared. When en-
abled then the link will be treated like a folder and it will be scanned recursively. (Note
that the program doesn’t check if the link is ˝recursive˝. So for example a folder that con-
tains a link to the folder would cause an infinite loop, and after some time when the stack
overflows or all memory is used up, crash the program.) Default is off.
Case sensitive filename comparison
Default is false on Windows® , true for other operating systems.
File Comparison Mode:
Binary comparison
This is the default file comparison mode.
Full analysis
Do a full analysis of each file and show the statistics information columns. (Number
of Solved, Unsolved, Nonwhite and White conflicts.) The full analysis is slower than
a simple binary analysis, and much slower when used on files that don’t contain text.
(Specify the appropriate file-antipatterns.)
37
The KDiff3 Handbook
Usually the folder-merge list view remains visible while a single file is compared or merged. With
the mouse you can move the splitter bar that separates the file list from the text-diff windows.
If you don’t want this, you can disable Folder → Folder && Text Split Screen View menu item.
Then you can use Folder → Toggle View menu item to switch between the file list and the text-
diff view that then occupy the full screen.
Probably you will prefer a simple double mouse click on a file in order to compare it. Never-
theless there also exists an entry in the Folder menu. You can also directly merge a single file
by selecting it and choosing Merge → Merge Current File menu item. On saving the result, the
status will be set to done, and the file will not be merged again if a folder merge is started.
But note that this status information will be lost when you rerun a folder scan: Folder → Rescan
Sometimes you need to compare or merge files with different names (e.g. the current file and the
backup in the same folder).
Select the exact file by clicking onto the icon in the column A, B or C. The first file selected thus
will be marked with an A, the second and third with B and C regardless on what column they
are in. Only up to three files can be chosen like this.
38
The KDiff3 Handbook
Proceed by choosing Folder → Compare Explicitly Selected Files or Folder → Merge Explicitly
Selected Files menu item. For your convenience these menu entries also appear as context menu
when you right-click the last selected file.
The comparison or merge of a file will happen in the same window. If this method is used for
folders a new window will be opened.
39
The KDiff3 Handbook
Chapter 4
Miscellaneous Topics
4.1.1 KIO-Slaves
The KIO library from Frameworks supports network transparency via KIO-slaves. KDiff3 uses
this for reading input files and for scanning folders. This means that you can specify files and
folders on local and remote resources via URLs.
Example:
kdiff3 test . cpp ftp :// ftp . faraway . org / test . cpp
kdiff3 tar :/ home / hacker / archive . tar . gz / folder ./ folder
The first line compares a local file with a file on an FTP server. The second line compares a folder
within an compressed archive with a local folder.
Other KIO-slaves that are interesting are:
• Man-pages (man:),
• Info-pages (info:),
40
The KDiff3 Handbook
Network transparency has one drawback: Not all resources have the same capabilities.
Sometimes this is due to the file system of the server, sometimes due to the protocol. Here is a
short list of restrictions:
Then to see the difference between two commits use git difftool first_hash second_h
ash --tool=kdiff3 --cc some_file_in_the_git_tree
To merge a branch with KDiff3 use git merge branch_name && git mergetool --tool
=kdiff3
After resolving merging conflicts in the usual way it is enough to commit the changes to do the
job.
41
The KDiff3 Handbook
Chapter 5
This document may have been updated since your installation. You can find the latest version at
[Link] .
42
The KDiff3 Handbook
when not doing if ˝Full analysis˝ was also off. However, with Full analysis on KDiff3
would initially attempt to compare the path pointed to by a link rather than follow it. Prior
to 1.8 the resulting error was ignored. However, 1.8 began reporting the error.
8. Why does the editor in the merge result window not have an ˝undo˝-function?
This was too much effort until now. You can always restore a version from one source
(A, B or C) by clicking the respective button. For big editing the use of another editor is
recommended anyway.
9. When I removed some text, then suddenly ˝<No src line>˝ appeared and cannot be deleted. What
does that mean and how can one remove this?
For each equal or different section the editor in the merge result window remembers where
it begins or ends. ˝<No src line>˝ means that there is nothing left in a section, not even a
new line character. This can happen either while merging automatically or by editing. This
is no problem, since this hint won’t appear in the saved file. If you want the original source
back just select the section (click on the left summary column) and then click the source
button with the needed contents (A/B or C).
10. Why doesn’t KDiff3 support syntax-highlighting?
KDiff3 already uses many colors for difference highlighting. More highlighting would be
confusing. Use another editor for this.
11. Can I use KDiff3 to compare LibreOffice/[Link]® , Microsoft® Word, Microsoft® Excel,
PDF, etc. files?
Although KDiff3 will analyse any kind of file the result will probably not be very satisfac-
tory for you.
KDiff3 was made to compare pure text files. LibreOffice/ [Link]® , Microsoft®
Word, Microsoft® Excel, etc. store much more information in the files (about fonts, pictures,
pages, colors, etc.) which KDiff3 doesn’t know about. So KDiff3 will show you the contents
of the file interpreted as pure text, but this might be unreadable or at least it will look very
odd.
Since most programs nowadays store their contents in XML format, you might be able to
read it as pure text. So if the change was only small, KDiff3 still might help you.
The best solution if you only want to compare the text (without embedded objects like
pictures) is to use Edit → Select All and Edit → Copy menu items in your program to copy
the interesting text to the clipboard and then in KDiff3 paste the text into either diff input
window. (See also Select, Copy And Paste.)
12. Where has the folder option List only deltas gone?
There are now several ˝Show˝ options in the Folder menu. Disabling Show Identical Files
will achieve what enabling List only deltas used to do.
13. How can I make a big selection in the diff input window because scrolling takes so long?
Start the selection as usual (click and hold the left mouse button). Then use the navigation
keys (e.g. PgUp, PgDn) while holding the left mouse button down. (See also Select, Copy
And Paste.)
14. There is so much information here, but your question is still not answered?
Please send me your question. I appreciate every comment.
43
The KDiff3 Handbook
Chapter 6
44