Is there a way to always run the same file in TextMate, when working on a Python project? I always press +R when editing a random file, forgetting to switch back to the tab containing the main of this project. There's got to be a way to do that; clicking tabs is really a speed-bump in that scenario.

  • To shorten that speed bump slightly, perhaps keep the main file as the first tab, then quickly jump to it before running your script with ⌘1 before hitting ⌘R. Combined, it's pretty fast. However, I wouldn't mind a solution to this myself. Sort of like %!TEX root for TeX files in TextMate and TeXShop.
    – fideli
    Aug 22 '10 at 5:20

Yes it is possible and there are several possibilities to achieve that. An easy fix would be to write a command which invokes python on the main file, i.e.

python /path/to/project/main.py

as a command bound to Command + R. Better commands would be

python $TM_DIRECTORY/main.py


python $TM_PROJECT_DIRECTORY/main.py

However, you will loose the nice looking output the default command provides. Therefore you have another option: Set the instance variable $TM_PROJECT_MASTER to point to your main file. You can either set this variable absolute or relative to $TM_PROJECT_PATH. A relative path may be advisable of course. To do so, you may either set $TM_PROJECT_MASTER as a static environment variable, which can be done as follows

  1. Go to "Settings" > "Advanced" > "Shell Variables"
  2. Add the variable "$TM_PROJECT_MASTER" and type in "main.py" or whatever is appropriate for your projects

If the location of your main file changes from project to project, this setting doesn't help. However, you can set project dependent shell variables, which would be the solution in such cases:

  1. deselect alls files in your project drawer
  2. click the small info button - a window with project dependent shell variables will pop up
  3. again add the variable "$TM_PROJECT_MASTER" and fill in the relative position of your main file

This should help. Have fun!

PS: See https://github.com/textmate/python.tmbundle/commit/c3e38e318c5e80b8fdd78730d65f1a7dee5aa911 if you look for an explanation, why setting $TM_PROJECT_MASTER helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.