SHARE:
Uncategorized

Sublime Text Demo: How the f did they do that? Part 2

Flatiron School / 14 November 2013

The following is a guest post by Theo Vora and originally appeared on his blog. Theo is currently in the Ruby-003 class at The Flatiron School. You can follow him on Twitter here.

If you were blown away by the 6 demonstrations on the Sublime Text 2 website like I was, you were probably wondering, “how did they do that?”

I covered how to do the first 3 demos in detail in a previous blog entry. Check out part 1.

In this blog, I cover the last 3.

4. Goto Anything – a file

Keyboard Shortcut

Mac: Command P

Win/Linux: Ctrl P

Order of Events

  1. Press ⌘P
  2. Type loa. These are the first 3 letters of ‘loader.py’ which Sublime Text returns to you.
  3. Press Enter. ‘loader.py’ opens.
  4. Press ⌘P
  5. Type hutil. Notice that the file ‘utils.py’ from the http folder is returned. Apparently, django has more than 1 ‘utils.py’. There’s another ‘utils.py’ in the ‘db’ directory. What would you type to get the db file? 😉

Why you would use this

Have you ever had to “drill” into directory just to open up one file? By “drilling”, I mean you open subdirectory after subdirectory until you find the file you want to open. Sometimes, my initial drill doesn’t even yield the file for which I’m looking! I have to drill somewhere else!

This is a huge timesaver. Sublime Text 2 eliminates the need for you to remember the exact location of a particular file. Just press this shortcut, type any 3 letters of the file you want (it could even be con of ‘application_controller.rb’), use the up and down arrows to change your selection, then press enter to open your selection.

5. Goto Anything – within a file

Keyboard Shortcut

Mac: Command P

Win/Linux: Ctrl P

Syntax

Order of Events

  1. Press ⌘P. The Goto Anything pane appears.
  2. Type loa. Sublime Text highlights ‘loader.py’. Notice you can see the contents of the file that’s currently selected in the text editing pane.
  3. Type @. At this point in time, the Goto Anything pane shows the list of functions inside of ‘loader.py’.
  4. Type ftl. Sublime will highlight the function named def find_template_loader.
  5. Press Enter to see this function highlighted in the text editor.

Why you would use this

During development, you’re always running into errors. The errors are usually helpful in telling you where the problem occurred – the file name is given and sometimes with a line number or function/method name.

If the file in question is not open, you can easily open it with ⌘P and add tack on @ along with your function/method name to jump to it quickly.

6. Find and Replace with regular expressions

Keyboard Shortcut

Mac: Command F

Win/Linux: Ctrl F

Order of Events

  1. Press ⌘F. The find pane appears at the bottom of the Sublime Text window.
  2. If it’s not already selected, press the Regular expression button. It’s the left-most button in the Find pane.
  3. Type “ +” (yes, there’s a space before the plus sign). This highlights all occurrences of one or more spaces.
  4. Press Find All. This selects all occurrences of one or more spaces.
  5. Press right arrow (→) once.
  6. Press Backspace once. (Macs press Delete)
  7. Press Esc to exit multi-cursor mode.

Why you would use this

Could you imagine changing all of the double spaces to single space in that document manually?

Granted, you didn’t need a regular expression to solve that particular example. You could have just done a normal Find All (with the regular expression button turned off) on double spaces.

Sooner or later, as a programmer, you will have to reformat a large body of text. Regular expressions allow us to find patterns of text. I’m not prepared to give a talk on regular expressions in this blog, but it’s definitely useful to understand how they work (I’ve listed a few tutorials below). It’s awesome that Sublime Text 2 allows you to toggle regular expressions in your search if you choose.

Here are a couple of tutorials on regular expressions. I haven’t completed them myself, as I learned them in school a long time ago. In the interest of being helpful, I liked these 2.

http://regexone.com/

http://regex.learncodethehardway.org/book/

Naming the World Previous Post Demystifying Singleton Classes and Methods Next Post