Writing Code

How to open a Swift file on a Mac

You can do this with Note My, without launching a development environment first. Here is how a lone .swift file from a repository, a gist or a colleague opens, reads, edits and completes.

You have a single .swift file. It came out of a repository, or a colleague sent it, or you pulled it from a gist to read one function. You do not want to create a project or wait for an index to build. You want to read the file, understand it, perhaps change three lines, and save it.

This guide covers opening a Swift file in Note My, confirming it is being read as Swift, the help you get while typing, and the handful of moves that let you cross a long file quickly.

Note

Everything described here happens on your Mac. There is no account to create, nothing to sign in to, and your code is never uploaded — the file you opened stays where you opened it.

Open the file

  1. Open it the ordinary way

    Press ⌘O and choose the file, or drag it from the Finder onto the tab bar. Drag several at once and each arrives in its own tab. Right-clicking in the Finder and choosing Note My ▸ Open works too, and that menu offers Open in Background as well.

  2. If the file has no extension, force it open

    Press ⇧⌘O for File ▸ Open Anything as Text…. This is the one to reach for when Swift source has been saved as .txt, renamed to .bak, or has no extension at all.

    Note My does not take files over. It opens what you point it at and leaves your defaults alone.

  3. Look at the language pill

    The status bar carries a language pill naming the language in use. For ContentView.swift that says Swift, and the colouring is already on. If it says Plain Text, you can set it yourself in two clicks — see below.

How to read the language pill

The pill is worth a glance before you start editing, because the language it names is the one you get the help for: the colouring, the suggestions and the call tips. Our advanced language identifier fills it in for you, so a .swift file is ready to read as soon as it is on screen, with no setting up first.

There are two things it can say, and each has a clear next step. Swift means you are set: the colouring is on and you can start reading. Plain Text means the file is open and safe to edit, and one pill away from the full Swift treatment. Setting it yourself takes two clicks, and the next section is where they are.

Plain Text is not an error and nothing needs fixing before you can work. Save, search, replace and every other text command behave the same either way. The only difference is the extra help, and you can switch that on yourself in a moment.

Setting the language yourself

Click the Identify pill in the status bar. A small card opens reading "Identified programming language: X", with an override pop-up and Confirm / Cancel. Choose Swift, confirm, and the colouring switches immediately. On an empty document the same card reads "Set programming language" instead.

Six counters, live: characters, words, spaces, lines, the size of the selection, and how many times the selected word occurs. They are cached on the document, so a keystroke does not recount two million characters.

Two related behaviours are worth knowing:

  • Paste Swift code into a brand-new, empty tab and the language is set for you once, quietly, with no sheet. Once you have chosen a language yourself, it never re-prompts.
  • Paste into a tab that already has a language and your choice stands: an accent dot appears on the Identify pill to say a suggestion is waiting, and it applies nothing until you open the Inspector and decide. Being asked on every paste is a setting, off by default.

Colour on screen, plain text on disk

Swift is one of 15 code languages our syntax engine colours in full. Types, numbers, escapes, string interpolation and comments each get their own treatment, which is most of what makes an unfamiliar file readable at speed.

The colour is never written into your file. What you save is exactly what you typed, so a file you edit here goes straight back into a repository with nothing extra riding along. If you want the colours in an output — a review document, a slide — use File ▸ Export As ▸ HTML with Syntax Highlighting… or the RTF and PDF variants beside it. Those are separate files; the original is untouched.

Suggestions while you type

As you type an identifier, a floating panel offers matching symbols: well-known names from Swift itself, things declared in the file you have open, language keywords, file paths, and words already in the text. Press ⌃Space to open it at any time, including when suggest-while-typing is off. Everything it offers comes from your Mac, so it works with the network off and there is nothing to sign in to.

The details that matter in practice:

  • The panel never takes the keyboard from you. Every keystroke reaches the document, so you can ignore it entirely and keep typing.
  • The list stays short enough to scan and scrolls if there is more, so it never buries the code beneath it.
  • You do not have to type a prefix: pri reaches printf, so a name you half remember is still worth typing. The names you use most are easy to reach again.
  • Tab and Return both accept the highlighted item, and each can be switched off independently in Settings ▸ Code Suggestions (⌘,).
  • When nothing matches, the panel steps quietly out of the way and leaves you typing.

The import line arrives with the symbol

This is where Swift gets something extra. Auto-import covers four languages — Swift, Python, JavaScript and TypeScript — and it offers a symbol together with the import line needed to use it. Accept the suggestion and the import line goes in at the top; you carry on typing where you were, without scrolling up.

It earns its keep in the situation this guide is about: you want to try one line and cannot remember which module the type lives in. Auto-import draws on a curated set of well-known symbols, which keeps the list quick to scan and the answer predictable. Those rows sit in their own labelled section, so you can see at a glance that an import line is coming with the name, and the preview shows you the text before you commit to it. It has its own switch in Settings ▸ Code Suggestions, and it is enabled per language.

Reading a signature as you write a call

Type an opening parenthesis and a small panel shows the signature with the argument you are on highlighted. Press ⌃⇧Space to summon it deliberately. It is the fastest way to check the order of two arguments without leaving the line you are writing. When there is a signature to show you see it, and when there is not the panel stays away rather than offering something invented — so what it tells you can be trusted. Call tips come with Swift, as they do across the code languages.

Filling in placeholders with Tab

Some suggestions arrive from our template library already filled in, with the first argument selected so that typing replaces it. Press ⇥ to jump to the next placeholder; that one is selected too, so you type straight over it. The last stop leaves the caret where you will carry on writing. Move the caret out of the expanded text and the run of placeholders ends.

Folding a type, and following a name

Double-click a brace and everything between it and its partner collapses out of sight — useful for skimming a long struct down to its method names. Double-click it again, or click where the folded text was, and it comes back. Three pair types fold: {}, () and [], and the gutter marks every foldable block.

Folding never changes your file. Nothing is removed, the document is not marked as edited, and saving a file with half of it folded away saves the whole thing. It is a way of looking, not an edit.

Double-clicking somewhere that is not a bracket does something different: the selection snaps to the whole identifier and every other occurrence of it is highlighted. Whole names only, so count does not light up inside counter — useful for tracing a variable through a long function. The Occ counter in the status bar tells you how many times the selected word appears, so a glance answers the question you would otherwise open a search for. Select a single word to read it.

Starting a Swift file from scratch

There are 40 built-in templates in four groups, and main.swift is one of them. Reach them from File ▸ New from Template or the Templates pull-down in the toolbar. The same menu inserts smart tokens such as {{DATE}}, {{AUTHOR}} and {{FILENAME}}, which our template library fills in as the template lands, so a new file arrives already headed and dated. When you save, the panel pre-fills the right extension for the language you are in, so a file you started as Swift reopens as Swift.

Very large generated Swift files

Generated sources and concatenated files can be enormous. Our large-file engine takes these on, so they open, scroll and search like any other document — no waiting about before you can read the thing, and no need to split it up first. The status bar counts characters, words, spaces and lines for the whole document rather than the part on screen, so you always know the size of what you are holding, and you can quote the figure to whoever asked for it.

The things you came for hold up at that size. Colour is what people expect to lose first in a file this long, and you keep it: our syntax engine goes on colouring wherever you are in the file, so a generated type stays as readable at line four hundred thousand as at line four. A file of that length is still typed into smoothly, and still saved with exactly the bytes it had. Our find and replace crosses the whole document at speed, which is usually how you get about a file too long to scroll: search for the type name, land on the declaration, read from there. Press ⌃Space wherever the caret is and the suggestion panel comes up on demand. And every part of it runs on your Mac, with nothing uploaded, however large the file is.

Troubleshooting

The status bar says Plain Text, not Swift

This turns up most often with a file that reached you under another name — as .txt, as .bak, or with no extension at all. Nothing is wrong and nothing is lost; you simply do not have the Swift help yet. The fix takes a moment: click the Identify pill, pick Swift, confirm. The colouring switches at once and the choice sticks for that tab, so you can carry on reading where you left off.

Nothing appears when I type

Three things bring it back. Press ⌃Space, which calls the panel up wherever the caret is and is the quickest route to it on a very long document. Then check that the kind of suggestion you are expecting is switched on in Settings ▸ Code Suggestions, since each has its own switch. And remember that a panel with nothing to show closes quietly, so "nothing appeared" and "nothing matched" look the same from the outside — type a shorter prefix and watch the list fill up.

Landing on the row you meant

The list is grouped into labelled sections and the highlight starts on the first row of the first section, so it is worth a glance before you press Return. Two moves get you to the row you want. Type another character or two and it rises towards the top — usually quicker than reaching for the arrow keys — or step down to it with ↓, which is the one to use for the last row or two. Type the name the way you would write it, capitals included, and the row you meant comes up to meet you. Where two rows look alike, the preview shows you what is about to go in and the section label above each one tells you which is which: a standard-library call, one of your own declarations, or a name arriving with its import line.

Double-clicking folded the file instead of selecting a word

That is how double-click is meant to behave. On a bracket it folds or unfolds that block; anywhere else it selects the identifier under the pointer and highlights its other occurrences. Double-click the same brace again to bring the text back.

Tab is not accepting the suggestion

Accepting with Tab and accepting with Return are separate switches in Settings ▸ Code Suggestions, and either can be off. Tab is also what advances template placeholders, so inside an expanded snippet it moves to the next stop rather than accepting anything.

I re-indented the file and want it back

The copy on disk is your way back, and it is a dependable one. Save before you press Apply. Then, if the layout our indentation engine produced is not what you pictured, File ▸ Reload from Disk (⌘R) brings the saved file into the tab exactly as you left it and you can run it again with different settings. Reloading never changes the file on disk, so you can go round that loop as many times as it takes to land on the layout you want.

The file changed on disk while I had it open

You are told. A sheet offers Reload or Keep My Version; keeping yours means the version in front of you is the one that counts. If the tab is in the background the prompt waits until you switch to it, so a file you are not looking at is never changed under you. Our auto-recovery safety net covers the rest: quit unexpectedly and your unsaved work is still there when you come back.

What you can do with one Swift file

Everything above comes down to a short list, and it is worth reading in one piece — this is the whole of what a lone .swift file gives you here, from the moment it lands to the moment you hand it on:

  • Read it the moment it opens. Colouring is on as soon as the file is on screen, so an unfamiliar type or a long switch resolves at a glance instead of after a wait.
  • Edit it and put it straight back. What you save is the characters you typed and nothing else, so the file returns to the repository carrying only the lines you changed.
  • Skim a long type down to its shape. Double-click a brace and the block folds away, leaving the method names; double-click again and it is back. The gutter marks every block you can collapse, and {}, () and [] all fold.
  • Trace a name through a function. Double-click the identifier and every other occurrence lights up, whole words only, with the count waiting in the status bar. It is a way of looking: the document itself is untouched.
  • Change that name throughout. Our find and replace runs across the whole document, however long it is, and it is as good a way of reaching a declaration as it is of rewriting one.
  • Type with the file helping you. Suggestions drawn from the file in front of you and a catalogue of well-known symbols, the import line arriving alongside the symbol, a signature under the caret as you write the call, and ⇥ walking you through the placeholders.
  • Start a new one from a template. main.swift sits in the built-in catalogue and arrives headed and dated, with the right extension pre-filled when you save.
  • Put two files side by side. Multi View gives each open tab its own pane, so the Swift file and the one it talks to can be read together.
  • Hand it on with the colours intact. Export to HTML, RTF or PDF for a review document or a slide, and the original file stays exactly as it was.
  • Keep all of it to yourself. No account, no sign-in, nothing uploaded. The file you opened stays where you opened it, and the app is just as useful with the network off.

That is the shape of the thing: one file, open in a second or two, readable straight away, and closed again without a trace in the folder around it. It suits the job this guide opened with — someone sends you a Swift file, you want to understand it, change three lines and get on with your day.

Every shortcut and gesture used in this guide, with what it does
TaskHow
Open a Swift file⌘O, or drag it onto the tab bar
Open Anything as Text…⇧⌘O
Save⌘S
Set the language yourselfClick the Identify pill, choose Swift, confirm
Open the suggestion panel⌃Space
Show the call signature⌃⇧Space
Jump to the next placeholder⇥
Fold or unfold a blockDouble-click either brace of the pair
Highlight every occurrence of a nameDouble-click the name
Suggestion settingsSettings ▸ Code Suggestions (⌘,)
Reload the saved copy⌘R

Frequently asked questions

Do I need a project or a folder to edit one Swift file?

Just the file itself. Open it, edit it, save it with ⌘S, and you are done. Nothing is generated beside it, and closing the tab leaves the folder exactly as you found it.

Will this take over as the default app for my .swift files?

Your defaults stay exactly as you set them. Note My appears in the Open With menu when you want it and leaves it at that: it never makes itself the default, and installing it changes nothing about what opens when you double-click. Forcing a file open is something you ask for with ⇧⌘O; it never happens on its own.

Can I open Swift source that has lost its extension?

Yes. Press ⇧⌘O for File ▸ Open Anything as Text… and pick the file, whatever it is called — .txt, .bak, or nothing at all. Once it is on screen, click the Identify pill, choose Swift and confirm, and you have the colouring, the suggestions and the call tips on a file that arrived with no clue to its own contents.

Does this sit alongside my usual build tools?

Yes, and that is the natural way round. Open the file here to read it, change it and save it; what lands on disk is precisely what you typed, so the file goes back into your build carrying nothing but your edits. Read the colouring as a map of the structure, and let your toolchain have the last word on whether it compiles.

Does the syntax colour get saved into the file?

Never. The document keeps only your characters, so a file edited here is byte-for-byte the file you meant to write. For coloured output, export it: File ▸ Export As ▸ HTML with Syntax Highlighting… produces a single self-contained file you can send on, and there are RTF and PDF equivalents.

Where do the completion suggestions come from?

From your Mac only: the file you have open, and a catalogue of well-known symbols that ships with the app. Nothing is looked up over the network, there is no account, and no part of your code leaves the machine.