Easy steps to compile and run C++ code and display the terminal for input/output. Specially useful for competitive programming.

  • Open Textmate
  • Go to Bundles -> Edit Bundles...
  • From the list choose C.
  • Then Menu Actions -> Run
  • In the editable panel at the bottom you will see some Ruby code by the author of the Bundle. Delete all of that and add this lines of bash code:
#! /bin/sh

file_path="$(dirname "$TM_FILEPATH")"
file_name="$(basename "$TM_FILEPATH" .cpp)"

# you can use gcc instead or add any other compiler flags
cmd_to_run="cd $file_path; c++ $TM_FILEPATH -std=c++11 -o $file_name.out; ./$file_name.out "

# applescript code to open the terminal and execute command
osascript -e "tell application \"Terminal\" to do script \"$cmd_to_run\""

  • On the left panel , set Output to Discard. This will avoid the default TextMate widget to spawn. Save, and you are done.

You can know do ⌘ + R to run your file, any compilation message will show in the terminal.