Text Editor

Sublime Text all the way

We recommend using Sublime Text as a text editor, which you can download and run on Mac OSX, Ubuntu, or Windows. This is an editor that has been specifically designed for writing code, comes with a lot of great tools, and is easily customized.

Good editors like Sublime help us format things. For example, when we ’re on a line that starts with def something and hit “return” at the end of the line, Sublime will already indent the next line by two spaces. If we now type end, then Sublime notices that we’re closing the method and outdents it again so that def and end sit on the same level.

Notice also that when we type an opening parenthesis, (, Sublime adds a closing one automatically.Be sure to keep the cursor placed between them, though, so we can type the argument list where it belongs.

Discover some more text editors

Other text editors that might be worth looking at are Atom and Textmate 2, and, if you like to, you can use some of the powerful old-school tools from the early times of Unix, VIM and Emacs. These are all great editors to use.

Let’s delve into semantics

Whatever editor we use, we want it to insert two spaces when we hit the “tab” key to indent our code. We should make sure our editor is configured to do this.

For Sublime Text, we can do the following. In the menu item “Sublime Text,” go to “Preferences” and select “Settings - User.” This opens up a configuration file that we can edit just like any other file. Ensure it looks like this:

Press + to interact
{
"tab_size": 2,
"translate_tabs_to_spaces": true
}

Also, we recommend enabling auto-saving our files. This automatically saves changes when we switch to another application (like our terminal) and protects us from the mistake of forgetting to save:

Press + to interact
{
"tab_size": 2,
"translate_tabs_to_spaces": true,
"save_on_focus_lost": true
}

Whenever you open a new file, make sure to save it with a file name that ends with .rb first. This will tell the editor that you want this to be a Ruby file. Your editor will start highlighting your code as Ruby code and enable other Ruby-specific editor features. Alternatively, select “Ruby” in the extensions menu at the bottom right.

Some useful keyboard shortcuts

  • To indent or unindent code, select the lines and hit “Tab” or “Shift-Tab”, respectively.

  • To comment lines of code, select them and hit “cmd-/” on Mac OSX or “ctrl-/” on Linux/Windows.

  • Cut out code with “cmd-x”, copy code with “cmd-c”, and paste it with “cmd-v” on Mac OSX. On Linux/Windows, use the “ctrl” key instead.