Here are ten more items I find worth knowing about Eclipse. Parts 1 and 2 are also on this blog in the form of screencasts. Some (but not many) of these tips overlap with the screencast. Here's a table of contents to save you some scrolling:
- Getting the proper user name to show up
- Folding Code Regions
- Speed switching between source and header
- Getting Mylyn (Trac) Updates from ProjectLocker down to Eclipse
- Always run lint and make it convenient
- Look at your workspace settings
- Use Surround With
- Stop Manually Formatting your code
- Quick Outline is worth using (this is different than the outline view)
- Stop ignoring block selection mode
Actually I don't really like putting the username in the file (it's already in the source code management system that you're using right?), but someone asked me how to do it, so here it is: in the eclipse.ini file add :
Duser.name = John Q. Public
Now when you create a new class the name you want will show up in the comments. Also don't forget to use the Preferences pane to modify all those templates to your liking.
The short version is just us #if(1) //Name your region …C++ code …. #endif. The long version is a separate post.
After programming in C# exclusively for a while you really begin to hate the inconvenience of .h files. However, you can mitigate some of the pain by assigning a really convenient keyboard shortcut to the Toggle Source/Header command. I prefer to use my numeric keypad number 1.
Tip 4. – Getting Mylyn (Trac) Updates from ProjectLocker down to Eclipse
After making changes online via the web interface to the repository, for instance adding new entries to the component list, you need to get those changes into Eclipse. Use the view menu from the Task List window to show the Task Repository View. Then follow the steps shown below.
I know by now you've seen the light and you've set up lint to run in Eclipse, and you run it all the time. Don't keep using the menu to run it. I like to assign the numeric keypad 2 to run lint. To jump to this (or any keyboard setting) type the name of the command in the box above the list of commands. The only caveat here for running lint, is you can only assign a shortcut for the last launched external tool, so you do need to use the menu once after running any other external tool. Still I find this quite handy.
Since I have the external configuration for lint set to build the project before running, I don't want to have the code built automatically every time I save. I turn off that feature. While I'm there I turn ON the feature for Refresh automatically and I make sure the Save Automatically before build is turned on. I'm not sure why Refresh automatically is off by default. I never go outside Eclipse, modify a project file and DON'T want the change to be shown inside Eclipse. If somehow I need the old version I can always grab it from SVN or even the local copies Eclipse makes. If you don't want to do this at least assign a convenient key shortcut to the Refresh command.
Select the entire line of code you want to surround with code. Then hit the Content Assist shortcut (typically Ctrl-space) and start typing the template you want to surround the code with. For example, start typing "try" to see the tray/catch template, either type enough to highlight it or arrow down to it and hit enter. Type "if" to surround with an if statement. If anything goes wrong, you can just Ctrl-Z your way back to your original code. Java has a similar feature for removing a try or if, but I can't figure out how to do it in C++. Anyone?
Use the Windows->Prefs (use your keyboard shortcut) and select the Code Style section as shown. Hit Edit, put a new name in the Profile Name: box. Now start checking out all the tabs and configure everything to exactly conform to your whims (or corporate coding standards if you must). Now just type code any old way. When you're done hit the keyboard shortcut for Format and you're not only done quicker, your code will be way more consistent looking than when you were entering indentation and spaces by hand. If you don't know your shortcut look near the end of the Edit menu for Format or hit Shift-Ctrl-L once to bring up the shortcut browser.
If you watched my Eclipse Tips screencasts you know I like the outline view. Quick Outline is very useful too.Hit Ctrl-O in a C++ editor window, optionally type a few keystrokes to filter the results and either click or arrow down to the desired target. It won't allow sorting and code rearrangement the way the outline does but day to day it may be more useful. Also start your filter with * and you change from a begins with search to a contains search.
I was duplicating the Color structure from .NET into C++. I wanted a handful of predefined colors s I created some static members like so:
I copied and pasted this from the .h file into the .cpp file and I started to modify the data a line at a time when I remembered block selection mode (mentioned in the Tips #1 screencast). I turned on block selection mode and made a rectangular selection containing the word static and typed Color. I then made a rectangular selection of the spaces between the word Color and the name of the Color and typed two colons.
Finally I deleted all the semi-colons (one at a time) and then grabbed a rectangular selection of white space after the color names and typed once "Color= (0x0,0x0,0x0);" Then I hit reformat and was ready to type in the actual color values.
Note that in addition to changing your cursor block selection mode changes your default font. When you exit block selection mode your normal font returns. It seems like a lot of work when I write it out like this but it took all of a few seconds and saved loads of keystrokes.