SwiftFormat
A command-line tool and Xcode Extension for formatting Swift code
Install / Use
/learn @nicklockwood/SwiftFormatREADME
![]()
Table of Contents
What is this?
SwiftFormat is a code library and command-line tool for reformatting Swift code on macOS, Linux or Windows.
SwiftFormat goes above and beyond what you might expect from a code formatter. In addition to adjusting white space it can insert or remove implicit self, remove redundant parentheses, and correct many other deviations from the standard Swift idioms.
Why would I want to do that?
Many programmers have a preferred style for formatting their code, and others seem entirely blind to the existing formatting conventions of a project (to the enragement of their colleagues).
When collaborating on a project, it can be helpful to agree on a common coding style, but enforcing that manually is tedious and error-prone, and can lead to arguments if some participants take it more seriously than others.
Having a tool to automatically enforce a common style eliminates those issues, and lets you focus on the behavior of the code, not its presentation.
How do I install it?
That depends - There are several ways you can use SwiftFormat:
- As a command-line tool that you run manually, or as part of some other toolchain
- As a Source Editor Extension that you can invoke via the Editor > SwiftFormat menu within Xcode
- As a build phase in your Xcode project, so that it runs every time you press Cmd-R or Cmd-B, or
- As a Git pre-commit hook, so that it runs on any files you've changed before you check them in
Command-line tool
Installation:
You can install the swiftformat command-line tool on macOS or Linux using Homebrew. Assuming you already have Homebrew installed, just type:
$ brew install swiftformat
To update to the latest version once installed:
$ brew upgrade swiftformat
Alternatively, you can install the tool on macOS or Linux by using Mint as follows:
$ mint install nicklockwood/SwiftFormat
Or if you prefer, you can check out and build SwiftFormat manually on macOS, Linux or Windows as follows:
$ git clone https://github.com/nicklockwood/SwiftFormat
$ cd SwiftFormat
$ swift build -c release
If you are installing SwiftFormat into your project directory, you can use CocoaPods on macOS to automatically install the swiftformat binary along with your other pods - see the Xcode build phase instructions below for details.
Another option is to include the binary artifactbundle in your Package.swift:
.binaryTarget(
name: "swiftformat",
url: "https://github.com/nicklockwood/SwiftFormat/releases/download/0.55.0/swiftformat-macos.artifactbundle.zip",
checksum: "CHECKSUM"
),
If you would prefer not to use a package manager, you can build the command-line app manually:
-
open
SwiftFormat.xcodeprojand build theSwiftFormat (Application)scheme. -
Drag the
swiftformatbinary into/usr/local/bin/(this is a hidden folder, but you can use the Finder'sGo > Go to Folder...menu to open it). -
Open
~/.bash_profilein your favorite text editor (this is a hidden file, but you can typeopen ~/.bash_profilein the terminal to open it). -
Add the following line to the file:
alias swiftformat="/usr/local/bin/swiftformat --indent 4"(you can omit the--indent 4, or replace it with something else. Runswiftformat --helpto see the available options). -
Save the
.bash_profilefile and run the commandsource ~/.bash_profilefor the changes to take effect.
Usage:
If you followed the installation instructions above, you can now just type
$ swiftformat .
(that's a space and then a period after the command) in the terminal to format any Swift files in the current directory. In place of the ., you can instead type an absolute or relative path to the file or directory that you want to format.
WARNING: swiftformat . will overwrite any Swift files it finds in the current directory, and any subfolders therein. If you run it in your home directory, it will probably reformat every Swift file on your hard drive.
To use it safely, do the following:
-
Choose a file or directory that you want to apply the changes to.
-
Make sure that you have committed all your changes to that code safely in git (or whatever source control system you use).
-
(Optional) In Terminal, type
swiftformat --infer-options "/path/to/your/code/". This will suggest a set of formatting options to use that match your existing project style (but you are free to ignore these and use the defaults, or your own settings if you prefer).The path can point to either a single Swift file or a directory of files. It can be either be absolute, or relative to the current directory. The
""quotes around the path are optional, but if the path contains spaces then you either need to use quotes, or escape each space with\. You may include multiple paths separated by spaces. -
In Terminal, type
swiftformat "/path/to/your/code/". The same rules apply as above with respect to paths, and multiple space-delimited paths are allowed.If you used
--infer-optionsto generate a suggested set of options in step 3, you should copy and paste them into the command, either before or after the path(s) to your source files.If you have created a config file, you can specify its path using
--config "/path/to/your/config-file/". Alternatively, if you name the file.swiftformatand place it inside the project you are formatting, it will be picked up automatically. -
Press enter to begin formatting. Once the formatting is complete, use your source control system to check the changes, and verify that no undesirable changes have been introduced. If they have, revert the changes, tweak the options and try again.
-
(Optional) commit the changes.
Following these instructions should ensure that you avoid catastrophic data loss, but in the unlikely event that it wipes your hard drive, please note that I accept no responsibility.
Using Standard Input/Output:
If you prefer, you can use unix pipes to include SwiftFormat as part of a command chain. For example, this is an alternative way to format a file:
$ cat /path/to/file.swift | swiftformat --output /path/to/file.swift
Omitting the --output /path/to/file.swift will print the formatted file to Standard Output (stdout). You can also pass "stdout" explicitly as the output path:
$ cat /path/to/file.swift | swiftformat --output stdout
Or you can use > to specify the output path as follows:
$ cat /path/to/file.swift | swiftformat > /path/to/file.swift
If you do not supply an input file, SwiftFormat will automatically take its input from Standard Input (stdin), but will time-out if no input is received immediately and display the help screen. To make it explicit, pass "stdin" as the input path:
$ cat /path/to/file.swift | swiftformat stdin
When using stdin, SwiftFormat does not have access to the file path of the input, so features that rely on the file location (such as inserting the creation date into header comments, or detecting .swiftformat configuration files in the file path) will not work. To solve this, you can provide the file path using the --stdin-path argument:
$ cat /path/to/file.swift | swiftformat stdin --stdinpath /path/to/file.swift
Xcode source editor extension
Installation:
Like the command-line tool, you can install the SwiftFormat for Xcode extension application via Homebrew. Assuming you already have Homebrew installed, type:
$ brew install --cask swi
Related Skills
node-connect
335.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
82.5kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
335.2kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
82.5kCommit, push, and open a PR
