TechnologiesSwiftUI

TextEditingCommands struct

iOSmacOStvOSwatchOSvisionOS✓ renders

A built-in group of commands for searching, editing, and transforming

How it works

TextEditingCommands is a built-in command group that contributes the standard text-editing items — Undo, Redo, and the Find menu — to your app's menu bar. You add it to a scene's command set so that any focused text control, such as a TextField or TextEditor, gains the system's expected editing menu items and their keyboard shortcuts without you wiring up each command by hand. Reach for it when your app accepts editable text and you want the menu bar to match the platform conventions users already know.

  1. Conform to the Commands protocol

    TextEditingCommands is a struct that conforms to Commands, the protocol SwiftUI uses to describe menu-bar content. Because it is a self-contained command group, you never configure its body yourself — you instantiate it and let it supply the standard editing items. In the demo it backs an editable surface, the TextEditor(text: $draft) bound to the draft state, which is exactly the kind of control these commands act on.

  2. Add it with the commands(content:) scene modifier

    Command groups attach to a Scene, not a view, through the commands(content:) modifier on a WindowGroup or DocumentGroup. Inside that builder you write TextEditingCommands() to inject the Undo/Redo and Find items. The TextEditor shown in TextEditingCommandsDemo is the content those scene-level commands operate on once it has focus.

  3. Initialize with TextEditingCommands()

    The type exposes a single, no-argument initializer, TextEditingCommands(). There are no parameters to tune because the group's job is to present the platform-standard set; SwiftUI maps each item to its conventional shortcut (Command-Z for Undo, and so on) and enables or disables it based on whether an editable control like the draft-bound editor is active.

  4. Compose it with other command groups

    Because it is just one value conforming to Commands, you can list TextEditingCommands() alongside other groups such as TextFormattingCommands or your own CommandMenu in the same commands builder. SwiftUI merges them into a coherent menu bar, so the Edit menu items described in the demo's caption sit next to whatever else your scene contributes.

Try it — Type some text into the TextEditor(text: $draft), then choose Undo from the Edit menu (or press Command-Z) to watch the TextEditingCommands() group revert your last edit.

Example & preview

Press Run live & edit to compile it in your browser — then edit the Swift on the left and the preview re-renders live.

TextEditingCommands.swift
struct TextEditingCommandsDemo: View {
    @State private var draft = "The quick brown fox"
    var body: some View {
        VStack(alignment: .leading, spacing: 12) {
            Text("Edit menu commands")
                .font(.headline)
            TextEditor(text: $draft)
                .frame(height: 120)
                .border(.secondary)
            Text("TextEditingCommands adds the standard Edit menu (Undo, Cut, Copy, Paste) for this editable text.")
                .font(.caption)
                .foregroundStyle(.secondary)
        }
        .padding()
    }
}
Live preview
Edit menu commands The quick brown fox TextEditingCommands adds the standard Edit menu (Undo, Cut, Copy, Paste) for this editable text.
Edit menu commands The quick brown fox TextEditingCommands adds the standard Edit menu (Undo, Cut, Copy, Paste) for this editable text.
swift → lexer → parser → sema → uiir → canvas Open in Studio ↗
What's new in SwiftUI 27 →