Sublime Forum

Compile and run in Mac Terminal Sublime Text files


I’m working with a Mac and I want to compile and run my C files from the Terminal using a script. I’ve found a solution that apparently works quite well in Linux. Here is the article in Spanish (ayudasprogramacionweb.blogspot.c … 031640&m=1). It consists in two simple steps:

  1. You create a script in linux to compile and execute the source code in the Terminal and you store it in your Documents folder named as

#!/bin/bash gnome-terminal -e "/bin/bash -c 'gcc $1 -o $2; $2; echo; read -p 'Pulse_intro_pasa_salir...'; exit; exec /bin/bash'; &"

  1. Through a Sublime Text Build System, you call this script by passing as parameters the name of the source file.

{ "cmd": "~/Documents/ ${file_path}/${file_name} ${file_path}/${file_base_name}"], "shell": true }

I’ve tried it in my Mac but as I expected it doesn’t work… Sublime Text give me an error: “gnome-terminal: command not found”.
I have the gcc compiler installed and working and I have given permissions to the script using chmod +x ./Documents/
Could you help me to adapt it for Mac OS please? I’ve just started programming and I don’t know where to start to fix it…

Thank you very much!


Build System Output

Try either

    "cmd": "~/Documents/", "${file_path}/${file_name}", "${file_path}/${file_base_name}"]


    "shell_cmd": "~/Documents/ \"${file_path}/${file_name}\" \"${file_path}/${file_base_name}\""]


Thank you very much for your reply!
I’ve tried both options but they don’t work… With the first one Sublime Text return this error:

[Errno 2] No such file or directory: '~/Documents/' [cmd: ['~/Documents/', '/Users/Elle/Library/Application Support/Sublime Text 3/Packages/User/Hello.c', '/Users/Elle/Library/Application Support/Sublime Text 3/Packages/User/Hello']] [dir: /Users/Elle/Library/Application Support/Sublime Text 3/Packages/User] [path: /usr/bin:/bin:/usr/sbin:/sbin] [Finished]
With the second one Sublime Text doesn’t return any error, but nothing else either…
I have not experience but I think the problem is in the script not in the Build System…
Maybe I should change gnome-terminal for something else? I’ve also read the “command not found” error could be a problem with the PATH…



Well, the embedded Python can not find the file ‘~/Documents/’. Either it does not exists or it has problems resolving it somehow, maybe with the ~. Try another path or moving the file to a different place.



FichteFoll thank you very much for your help but after days of searching I’ve finally found a solution! In Mac there is no need of a script, all that you need is the proper Build System.

This one for C files:

"cmd": "bash", "-c", "gcc '${file}' -o '${file_path}/${file_base_name}' && open -a '${file_path}/${file_base_name}'"]

And this one for C++ files:

"cmd": "bash", "-cpp", "g++ '${file}' -o '${file_path}/${file_base_name}' && open -a '${file_path}/${file_base_name}'"]

Now, when I press Cmd+B, a new Terminal window opens and my file runs, and of course it accepts input from the user, which sublime text console doesn’t…

Simple and easy! All the credits to this guy: