If you’re not familiar with using Terminal and command line commands, here’s a quick intro for the purpose of our ksdiff article series.
Terminal is an app on your Mac, located in Utilities inside the Applications folder. Switch to the Finder, navigate to the Utilities folder (possibly via Go > Utilities from the menu), look for Terminal and double-click the app icon to start it.
Terminal operates on the level that all computers operated on in the past: the user instructs the computer by typing in commands, line by line, at a prompt, thus the term command line. All the computers you have ever used operate on this level, which is still key to how they work. Devices like the iPhone, Apple Watch and AirPods don’t expose this level to the average user. It’s not of interest to the casual Mac user, but it’s still very useful and powerful, which is why we still use it today.
You will be greeted by a window like in the following screenshot. It contains at least two lines of text. The first line is something starting with
Last login <some date> on console Just ignore for now. Below, you’ll see another line that may or may not contain all sorts of information. By default, it’s of the form
<user name>@<computer name> ~ % ▊
The important bit for us is the block at the end, called the cursor, the location where we are supposed to enter commands.
The way things work is that you type in one command, concluded pressing by the enter key (aka the return key) on your keyboard. The computer then carries out the command and returns with some result. Let‘s try.
ksdiff -v followed by the enter key. Make sure there is a space between ksdiff and the dash –, but no space between – and v. You need to be precise when entering commands. Immediately you will get a result, the current version number of ksdiff, i.e. 3.3 (146) as of the writing of this article. Using the command line, we asked ksdiff to output its version.
For the next step we assume you have two text files named file1.txt and file2.txt in your Documents folder, like shown in the next screenshot.
If you want to follow the tutorial and don’t have files ready to use, follow the steps below to create them. Get started by launching the app TextEdit in Applications.
- In TextEdit, create a new document via File > New
- Convert to plain text via Format > Make Plain Text
- Enter some text, maybe: Paragraph⏎⏎Paragraph file1⏎⏎Paragraph⏎
- Save via File > Save… Enter file.txt in Save As: and make sure to pick Documents next to Where:
- Repeat for file2.txt, this time entering Paragraph⏎⏎Paragraph⏎⏎Paragraph file2⏎
For now, we only need to learn one other command: cd, short for change directory. It’s like opening a different folder in Finder and setting the context to that folder. Let’s try by typing
cd Documents. Make sure to use the capital D in Documents. If it’s the first time you are doing that, you will be rewarded by a macOS system alert asking you for permission: “Terminal” would like to access files in your Documents folder. Click the OK button to confirm.
Now it’s time to use ksdiff to compare two files. Type
ksdiff file1.txt file2.txt. Make sure there is a space after ksdiff and enter the exact file names, i.e. no space in the file name and include and include the .txt extension at the end.
Kaleidoscope will open, showing you a comparison of those two files. Congrats, you learned the important first steps of using Terminal and the command line!
How apps and command line can interact
Did you notice, in the above example, when you typed
ksdiff file1.txt file2.txt into the command line in Terminal, Kaleidoscope opened as a result? This revealed an important power in Terminal commands: they can open normal apps, such as Kaleidoscope. This makes those commands very powerful, as they allow you to combine the power of the command line and apps you use.
To prove the point, let’s use another command. Enter
open -a Dictionary in Terminal and watch as the Dictionary app launches and shows its window.
This works in the opposite direction, too: you can get content from an app, such as Finder, into the command line.
Let’s try. Open a Finder window, navigate to your Documents folder (menu Go > Documents). Make the window small enough and move it to the right of your Terminal window, so that both windows are located side by side. Make sure the Terminal window is active.
ksdiff , make sure to have a space character at the end, but do not press enter. Instead drag file1.txt from that Finder window over to the Terminal window. When you let go and drop the file, you’ll see that a whole bunch of characters have been added to your Terminal command. That is the path to the file you just dropped, including the file name. Now repeat the drag-and-drop operation from the Finder window with file2.txt, dropping it again over the Terminal window. Now press the return key. Kaleidoscope opens and shows the differences between the two files, like it did in the example above.
Congrats! You just used two files from the Finder, via drag and drop, as input parameters to ksdiff. You learned quite a bit about Terminal and commands. You are also able to use any file from Finder as an input parameter to a command.
Now you are ready to read more about ksdiff and how to integrate it into your workflows.