Eclipse

This page collects some personal notes on configuring and using the Eclipse IDE. I post them here mainly for my own benefit, since inevitably at some point I will forget an important detail that took me a long time to sort out the first time.

You are welcome to refer to these notes yourself… at your own risk of course. (If you notice any errors, please submit feedback using the home menu above or by posting a comment in the box below.) However, there are plenty of other webpages that provide more comprehensive coverage of similar material. Some of these are listed in the References section at the bottom of this page.

  • Config

    The Eclipse IDE is great, especially if you are learning a new language. However, in my opinion, Eclipse is very ugly right out of the box.  Particularly annoying is the incredible waste of screen space, which is hard to accept if you ever program on a laptop.   Fortunately, Eclipse is highly configurable.

    Eclipse Themes

    1. Go to Help–>Eclipse Marketplace and enter “Color Theme” in the find box.
    2. Install two plugins called “Eclipse Color Theme” and “Eclipse 4 Chrome Theme.” (If you don’t see them, it probably means they are not compatible with whichever version of Eclipse you have installed.)
    3. Once the themes are installed, go to Window–>Preferences–>General–>Appearance. Select Chrome Theme or Color Theme and set the options you like.

    In particular, the Color Theme gives you options for darker color schemes like Zenburn, which might reduce eye strain. The Chrome Theme gives you options like “Sash Width,” allowing you to reduce the wasted space between workspace windows.

    Tip: If you choose a dark color theme, like Zenburn, the colors may not work well with the default colors used for syntax highlighting by the IDE plugins, like PyDev. For example, PyDev uses a light yellow to show matched strings which (in Zenburn at least) makes all matched strings practically invisible. To fix this, use Window–>Preferences–>Editors–>Text Editors–>Annotations. In the Annotations box on the right select Occurrences and either disable matching or choose a darker color. (See screenshot below.)

    Eclipse-PreferencesAnnotationsColorFix

    In the example below, I’ve used the eye-dropper tool to make matched strings appear in the same color as the current line color.

    Eclipse-ColorThemeFixed

    (In the window on the left, the cursor is currently at the variable max_par, so PyDev hightlights all nearby occurrences of max_par. Before changing the default “Occurrences” color used by PyDev, it was impossible to read the highlighted strings.)

  • PyDev console

    Once PyDev is installed, a Python interactive console (or REPL) can be configured as described on this page. Briefly, follow these steps

    1. From the Window menu, select Preferences–>PyDev–>Interactive Console.
    2. Check the box next to “Connect console to Variables Debug View?”
    3. Uncheck the box next to “Focus console when an evaluate command is sent from editor.”
    4. To use the console, select (highlight) a line of code in the Eclipse editor that you want to evaluate, then hit Ctrl+Alt+Enter. This brings up a window allowing you to choose which kind of interactive console you want.
  • Egit

    This section lists the steps I took to setup a git repository in the Eclipse IDE and host it on GitHub.
    The steps assume Eclipse is already installed. They are roughly the same as the steps given in the Egit github tutorial cited below in the References section, but in a different order.

    Git is a distributed revision control system invented by Linus Torvalds that has some advantages over client-server revision control systems like Subversion and CVS.

    GitHub is a web-based hosting service for software development projects that use git.

    The combination of git and GitHub makes it very easy to maintain, share, and collaborate on computer files and software projects. But even if you don’t expect others to use the stuff you host on GitHub, keeping your files organized in the cloud is very useful if you’re not always working in the same location on the same computer.

    GitHub is free for hosting publicly visible repositories. If you want private GitHub repositories, you must pay for the service. (Currently, this costs $7/month.)

    • Install Egit Eclipse plugin

      1. Help–>Install New Software
      2. From the “Work with” dropdown list select “–All Available Sites–“
      3. In the “Type filter text” box, enter Egit.
      4. Select “Eclipse Egit” (you don’t need the others).
      5. Click Next, accept the license, click Finish, then restart Eclipse.
    • Configure SSH keys

      1. In Eclipse, from the Window menu, select Preferences–>General–>Network Connections–>SSH2–>Key Management.
      2. Select “Generate DSA Key”, “Save private key”, and “OK” to save key without passphrase. Click OK to close the Preferences dialog.
      3. Login to your github.com account, then click the “Account Settings” icon in the top right of the github.com page.
      4. From the menu on the left of the Account Settings page, select SSH Keys. This should bring you to the page:
        https://github.com/settings/ssh.
      5. Select “Add SSH key” and copy-and-paste the contents of the id_dsa.pub file generated in Step 2 above into the Key box.  (You can see the contents of your id_dsa.pub file in a number of ways; e.g., open a terminal window and type `cat ~/.ssh/id_dsa.pub` or point a web browser to the address of the file, e.g., file:///home/yourusername/.ssh/id_dsa.pub.)
      6. After you have pasted your public key into the Key box on the github page, give the key a name in the Title box.  Probably you only want to do this once for all the repos you connect to Eclipse, so you might pick a name like “Eclipse”.
    • Create git repo in Eclipse

      1. In the Eclipse Package Explorer pane, right click on your project and select Team–>Share Project.
      2. Select Git then click Next.
      3. Next to the Repository field, click Create and type the name of your project in the Name box.  Select Finish.
      4. In the Eclipse Package Explorer pane, right click on your project and select Team–>Commit.
      5. Select only those program files that you want in the repository, type a commit message in the box, and click Commit.
    • Create and connect to a github remote repo

      In this section, we will create a remote git repository at github.com and connect it to the repo we created in Eclipse in the last step. Follow these steps to do this:

      1. In the Eclipse Package Explorer pane, right click on your project and select Team–>Show in Repositories View.
      2. Under References, right click on Remotes and select Create Remote.
      3. The Remote name should be origin and the Configure Push radio button should be selected. Click OK.  (Leave open the new dialog box that appears.  We’ll come back to it in a moment.)
      4. Open a web browser, login to your github.com account, and create a new repository.  (Do not select the option to automatically generate a README.md file.)
      5. When you create a new repo at github.com, and name it, say, myrepo, this produces a uri associated with the repo. If you select SSH as the transfer protocol the uri will be something like git@github.com:username/myrepo.git. Copy this uri with the copy button (or highlight–>right click–>Copy).
      6. Go back to Eclipse. In the “Configure push” dialog that we left open in Step 3 above, next to the URI box, click Change.
      7. In the Select a URI dialog, the URI box should have been set automatically to your github remote repository. In the Connection section of the Select a URI dialog, set the Protocol to SSH. (Leave Authentication as is, with User field git and Password field empty.)
      8. Select Finish–>Save (the button on the far left; i.e. do not select “Save and push”)
      9. Now, in the Git Repositories view, expand Remotes–>origin and you should see two ssh uri’s.  The green (fetch) and the red (push). Right click on the red uri and select Push.  The result should look something like this:
        Egit-remote-push
      10. Go back to your github.com page and check that the program files that you committed in the previous section have been pushed to the remote repository.
      11. Finally, make sure the git fetch function is configured properly. (The tutorial I followed implied that once you configured git push, Egit would automatically configure fetch, but for me this was not the case.) To configure fetch, go to the repositories view and expand Remotes–>origin. Right click on the green (fetch) arrow and select Configure Fetch from the popup menu. Change the settings so that the Configure Fetch box looks something like this:Eclipse-ConfigureFetchIn particular, the Refs mappings box might be empty (as it was in my case), so you will have to add one. Click Add and in the Source box type refs/heads/*, click Next then click finish. It should look like this before clicking Finish:

        Eclipse-AddingRefspecForFetch

        If this does not result in a Configure Fetch dialog like the one above, you can click the Advanced button and edit the Fetch Ref Specs dialog box so that it looks like this:

        Eclipse-FetchRefSpecs

    • Import, fetch, and pull code from github in Eclipse

      If you already have other repos on github and you want to import these into Eclipse, or if you want to import the repo you created above into Eclipse on a different computer, Quinn Liu has a nice 6 minute tutorial explaining how to do this, and how to fetch and pull updates.

  • Some useful git commands

    If you use Git through the Eclipse Egit plugin described above, the Team menu provides all of the Git functions you need. However, it is occasionally useful to do things from the command line. This section collects a few basic commands that are useful but easy to forget. (See also git — the simple guide.)

    1. List the currently set git parameters:

      git config -l
    2. Stop github from asking for your username/password on every push:

      git remote set-url origin git@github.com:username/reponame.git

      Note: if this is a brand new repo, you must use:

      git remote add origin git@github.com:username/reponame.git

      See also: this post.
  • node.js REPL in Emacs

    (This section obviously doesn’t belong on a page about Eclipse, but until I make a separate page about using Emacs as an IDE, I’ll keep miscellaneous notes about it here.)

    Emacs can be configured to provide a read-evaluate-print loop (REPL) for many languages. (See, for example, page 5 of Lecture 4, for node/javascript configuration). Once you have configured Emacs, the following are among the most useful commands for checking and running JavaScript code from within the Emacs editor:

    • C-c ! — bring up a node prompt in a *js* buffer (basically a JavaScript command line)
    • C-c C-j — send the current line in the current buffer to the command line in the *js* buffer
    • C-c C-r — send the currently selected region to the command line in the *js* buffer
    • C-c C-u — (assuming you have jshint installed) this checks the code in the current buffer for correctness
  • References

    1. Egit github Tutorial
    2. git — the simple guide
    3. The Pro Git book
    4. Quinn Liu’s 6 minute tutorial on importing a github repo in Eclipse.
    5. Lars Vogel’s Egit Tutorial
Advertisements

Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s