A nice feature in Eclipse that is somewhat hidden is the bookmarking feature. It lets you keep track of important places in your code and get back to them.
For example, let’s say you’re reading some code and you notice a nice algorithm. Or you’re looking for where something specific is happening, like calling a certain web service that is critical and you find it. Or whatever piques your interest.
You could rely on the navigational features of Eclipse to get you back to that code, but that could mean a lot of presses on the back button. You could put in a
// TODO , which adds an item to the Tasks view, but when you just want to remember where something is, that is an awkward way to get back to it.
So, instead, use Bookmarks. The only problem is that isn’t an obvious feature in Eclipse, like it is in say IntelliJ.
There’s really only a few things to learn though.
First, to add a bookmark, navigate to an interesting line of code, and then click on Edit | Add Bookmark… from the main menu. A dialog box will pop up asking you for a name. You should see a little blue bookmark appear in the left hand column and if you look carefully, you will also notice a little green rectangle in the right hand column.
Second, show the Bookmarks View. In my opinion, this should be on by default in the Java and Plug-in Development Perspectives, just like the Problems view. To see the Bookmarks View, select Window > Show View > Other… from the main menu. This will bring up a “Show View” dialog. Open up the “Basic” category, select “Bookmarks”, and press the OK button. You should see the bookmark you created in step one.
Third, when you find that you need to go back to your bookmark, just switch to the Bookmarks View and double-click on the bookmark you want to go back to. Note that from this view, you can also single-click to edit or delete with the red “X”. You can also sort and filter if you’ve got a lot of bookmarks.
Fourth, set up a keyboard shortcut for Adding a Bookmark. If you look at the useful Eclipse 3.0 full keyboard shortcut reference from the Eclipse Tutorial by Enrique (available in both Español and English.), you’ll notice that there is not a keyboard shortcut for Add Bookmark in the default Eclipse 3.0.1 install. (And it’s not there in 3.1M4 either.)
To get to the keyboard shortcuts, choose Window > Preferences from the main menu, which brings up the Preferences dialog. In 3.1M4, you can just type in “key” in the filter text box, which takes you to General>Keys. You then have to click the “Edit” button. In 3.0.1, you just navigate to Workbench>Keys.
Once you’re at the Keys preference, choose “Edit” from the Category drop-down. Then choose “Add Bookmark” from the Name drop-down. Then click in the Key Sequence > Name text box. Then hold down the keys you want to use for your keyboard shortcut. Being an Eclipse on Mac OS X user, I chose the available “Option-Cmd-B”. Then click the “Add” button. You should see the keyboard shortcut appear in both of the Assignments lists. (Note that “When: In Windows” always throws me off as a Mac user, but it just means not to use it within a dialog, which is OK since editors can only be in a window.)
Cool… now what? Well, it’d be nice if in Eclipse, we could:
1. Have bookmarks turned on by default, with the Bookmarks View showing, and a keyboard shortcut defined for all platforms for adding a bookmark.
2. Improve Bookmarks by adding an option to create a “quick” bookmark which didn’t need a description, like IntelliJ does. Allow quick navigation between bookmarks, at least in a file (previous/next bookmark in this file), like you can do with going to the next/previous error.
3. Allow sharing of bookmarks between team members. This might be overkill, since you can always tell people, go to this class and look at line #103, but it would be an interesting way to share code annotations.
1. If you hover over the blue bookmark marker in the left hand column of the editor, you’ll see a tooltip showing the description.
2. You can bookmark whole files by selecting the file in a view like the Package Explorer, then choosing Edit > Add Bookmark…