Grep Console How-to #1: The Basics

Here’s the first of the promised posts about Grep Console’s features. I’ve decided to do these as simple how-tos, and this first one covers the basics of highlighting console lines. If you’ve never used Grep Console before, this should show you all you need to get going. If, on the other hand, you’ve upgraded from Grep Console 2 to the new version 3, you’ll see how the new expression management differs from the previous incarnation. For this how-to, I’m assuming you’ve already installed Grep Console 3.

Grep Console is about finding and highlighting certain lines of text in Eclipse’s console. Before we can find and highlight lines, we have to get some into the console. Here’s a simple demo program to cover this how-to (I’m pasting only the main method):

public static void main(String[] args)
  System.out.println("This is the first line.");
  System.out.println("Some more text...");
  System.out.println("This is the second line.");
  System.out.println("Some more text...");
  System.out.println("This is the third line.");

Running this will show the following output in your console:

We want the “This is the…line” lines styled with different colours. There are three ways to do that – which is why I made the code output three lines. Let’s start with the simplest one. Open the Grep Console dialog by clicking the Grep Console icon in the console’s toolbar:

As you can see, there are already some demo settings provided by Grep Console upon installation. We won’t be using them, so instead create a new folder by clicking the “Add folder” button:

Here you can enter a name for the new folder. We’ll be using this folder for grep expressions specific for this how-to, so we disable both “active by default” and “filter by default” to prevent Grep Console from using the folder for all our projects. Clicking “Ok” adds the new folder.

Next, let’s add the first grep expression to the folder by selecting the new folder and clicking “Add expression”:

Each expression represents one grep expression that is tested against each line of console output. The most important field therefore is the “expression” field (the second line in the dialog). Here you can enter a regular expression. I’m assuming you’re familiar with at least basic regular expressions; if not, this Wikipedia article gives a basic overview, while this page from the Java API documentation describes the details of the regular expression syntax available in Java.

Grep Console will automatically add “.*” strings to the beginning and end of the expression entered here, so the simple expression “first” will be enough to find all lines containing the string “first”. The name above the expression is optional; whenever Grep Console lists an expression that has no name, it will show its regular expression instead.

We leave “active by default” and “filter by default” checked in this case: Since the new expression belongs to our “How-to 1” group that is disabled by default, other projects won’t be bothered by the new expression anyway.

To highlight the lines matched by our expression, we have to assign a style. Click “New” below the list of available styles to create a new one:

In this case, we should enter a name, as all styles without a name are listed as “(unnamed)”, which can become quite confusing if there are too many.

There are several options we can configure in our style. In this case, we’ll just select a background colour. You can do this by clicking the colour picker button to the right of the background field, or by entering a hex RGB value of a colour, or simply by activating the checkbox – Grep Console will pick a random colour if none has been specified yet.

Clicking “Ok” creates the new style. It now shows up in the list of styles on the right side of the expression dialog, and it has also been assigned to the “Whole line” group in the table of the left. This means that whenever Grep Console finds a line matching the specified regular expression, the whole line will be styled using the style we just created. I’ll be explaining groups in a later tutorial.

Now that our expression is fully defined, the preview on the lower left of the expression dialog already shows how our first console line is highlighted by the new style. Click “Ok” to create the expression and close the dialog.

We’re now back in the main Grep Console dialog – but unlike the expression window, this one doesn’t yet show any highlighting in the preview:

The expression dialog showed a working preview because it was a preview for that specific expression. The main dialog shows a preview of all enabled expressions, and as you recall, we set the “How-to 1” group to be disabled by default. While the expression itself is enabled, it’s still not used because it is part of an inactive group. This is also the reason why both the group and the expression are shown in a grey font.

Unlike in version 2 of Grep Console, where all settings were global, version 3 can use different settings for different launch configurations. Expression (and folder) items are still global, and modifying or deleting them will affect all launch configurations, but which items are enabled can be configured individually for each launch configuration.

If you look at the info panel at the top of the dialog, you will see that the dialog shows the specific settings for the launch configuration we used to start our demo program. This means that when we enable or disable items in this dialog, these settings only apply to this launch configuration. If you open the dialog without an active console (or through Eclipse’s preference dialog), there won’t be an active launch configuration, and you will be editing the global settings directly. On that note, the “active by default” and “filter by default” settings in the folder and expression dialogs always refer to the global settings.

Since the current dialog shows the settings for our own launch configuration, we can use it to enable the highlighting settings we created for our demo program. All other launch configurations will be unaffected by these settings – the “How-to 1” folder will still stay disabled for them.

To enable the folder, click once on the unchecked box  to the right of it (in the left of the two columns of icons). This will check the box, turn the folder and its expression black and highlight the first line in the preview:

The checkbox you just clicked has three states: An empty box means that the item is disabled for highlighting (in case of a folder, this also means that all of its contained items, regardless of their own activation settings, are disabled). A checked box means that the item is enabled for highlighting. A box between brackets (checked or unchecked) means that an item is neither specifically enabled nor disabled for the current launch configuration, and that its setting is taken from the item’s default setting. If you are editing the global defaults instead of a specific launch configuration, the normal checked and unchecked boxes already refer to the item’s default setting, and the third state (the box between brackets) is not available.

Having enabled our new folder, clicking “Ok” will confirm these settings and close the dialog. The console should now look like this:

This should be enough to get you started with Grep Console. The next how-to will show a few alternate ways to create a new expression.

This entry was posted in Grep Console and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *