Monthly Archives: August 2013

Sublime Text: Always run a single file in a project no matter what is focused

When I program, I often try to split up my code into modules. This requires that I use different files. However, I also want to run my code from a “main” or a “master” file as well. It’s pretty annoying in Sublime Text, or any text editor, to have to switch to your main file to run your project.

I’ve talked about how to do this in TextMate but how can you pull this off in Sublime Text? Let me show you how.

  1. Create a Sublime Text project for your program. You can do this by using the menu Project > Save Project As…”.
  2. Open the settings for your project using the menu Project > Edit Project.
  3. You’ll get a mostly empty text window. Modify this window to include a build definition, such as the following one for Python:
    • "build_systems": [
        {
          "name": "PyProj",
          "cmd": ["python", "-u", "$file"],
          "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
          "selector": "source.python"
        }
      ]
      
  4. Modify the line with “cmd” and change “$file” to the master file you always want to run. For example, if your main file is called pyproj.py, then change it to pyproj.py.
Add the "build_systems" JSON field in your project settings file, accessible through the Project menu > Edit project.

Add the “build_systems” JSON field in your project settings file, accessible through the Project menu > Edit project.

That’s it! While you’re editing your project settings, you can make other customizations as well as you see fit.

Obviously, this isn’t limited to just setting a file to be run no matter what file you’re focused on. You could use this to add command-line arguments to a single project, to run project-specific scripts for building and testing, or to otherwise customize the way the “Control-B” (Command-B on Mac OS X) works when you are running the project. You can essentially define your own build system on a per-project basis in a project file’s “build_systems” section and use any information in Sublime Text’s build system documentation to do so. Unfortunately, the build system documentation doesn’t really describe this because it’s focused more toward Package development.

For more information about how project settings work, look at Sublime Text’s documentation for build systems and for projects!

Advertisement

The Function (Fn) key is on the full-size Apple Keyboard

Are you one of those Mac users who generally turns on the F1, F2, keys as standard function keys option in System Preferences? If you do that, then if you want to use your keyboard to control the volume or the screen brightness, you need to find the Function (or Fn) key. (Alternatively, if you don’t turn it on then you need the Fn key to simulate F1, F2, and so forth keypresses).

Do you check off the box in System Preferences > Keyboard that makes the F keys behave as standard function keys?

Do you check off the box in System Preferences > Keyboard that makes the F keys behave as standard function keys? I usually do. I think I only ever use the Volume Control function on the keyboard anyway.

On the laptop and the wireless keyboards, Apple usually puts the Fn key on the lower-left hand corner, next to the “Control” key. But it’s not there on the full-sized Apple Keyboard with the numeric keypad!

Or is it?

The Fn Key on an Apple Keyboard is next to the home and above the delete key in the area above the arrow keys.

Ha, it’s in the middle of the keyboard between the letters and the numeric keypad, right below the F13 key and above the Delete key!