Skip to content

Instantly share code, notes, and snippets.

@Star-Lord-XIII
Last active June 21, 2021 01:52
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Star-Lord-XIII/838e7d767f41fef3f50d to your computer and use it in GitHub Desktop.
Save Star-Lord-XIII/838e7d767f41fef3f50d to your computer and use it in GitHub Desktop.
Running Swift scripts from Sublime Text 3 in MacOSX10.11

##Adding Swift Build System

  • Open Sublime Text 3
  • Go To Preferences > Browse Packages...
  • Add a file named Swift.sublime-build inside Packages directory.
  • Copy the following script in Swift.sublime-build file.
{
 	"shell_cmd": "xcrun swift $file",
 	"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
 	"working_dir": "${file_path}",
 	"selector": "source.swift",
}
  • Go to Preferences > Key Bindings-User and add the following script to it.
{
	"keys": ["super+b"], 
	"command": "build",
	"context": [
		{ "key": "selector", "operator": "equal", "operand": "source.swift" }
	], 
	"args": {
		"build_system": "Packages/Swift.sublime-build",
		"variant": "Build"
	}
}

You are ready to run Swift scripts from ST3 now.

##Testing the Swift Build System

  • Create a file A.swift containing a single line println("Hello, Swift!").
  • Press command + b and you'll be able to see the output.
  • If you are unable to see the output, go to Tools > Build Systems and change it from Automatic to Swift, and you should be able to run Swift in ST3.
@blairg23
Copy link

blairg23 commented Oct 1, 2015

Under "Testing the Swift Build System", the first bullet point should read: Create a file A.swift containing a single line print("Hello, Swift!").

"println" was renamed to "print" in current versions of Swift.

@a2v2
Copy link

a2v2 commented Nov 5, 2016

To ensure build no matter if the path to archive has blank space or not is to put double quotes on $file in the shell_cmd on the Swift.sublime-build file. Thanks for sharing your. script

The file would look like this:

{
 	"shell_cmd": "xcrun swift \"$file\"",
 	"file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$",
 	"working_dir": "${file_path}",
 	"selector": "source.swift",
}

@jandamm
Copy link

jandamm commented Apr 25, 2018

If you replace the shell_cmd with "xcrun swift -F . \"$file\"" you can add any framework into the folder of the file.
If you want to use a subfolder (eg "frameworks"), use "xcrun swift -F ./frameworks \"$file\"".

@gaet2006
Copy link

Hey, I'm on Ubuntu and I was wondering if you could help me out.
when I try to build the file it outputs this error:

bash: xcrun: command not found	
[Finished in 0.0s with exit code 127]
[shell_cmd: xcrun swift /home/gabriel/Documents/A.swift]
[dir: /home/gabriel/Documents]
[path: /home/gabriel/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]

I did some googling and found out that this is because xcrun is a dependant of xcode, which you cannot currently get on linux.
Is there any other way to do this? Thanks alot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment