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.
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 thedraftstate, which is exactly the kind of control these commands act on.Add it with the commands(content:) scene modifier
Command groups attach to a Scene, not a view, through the
commands(content:)modifier on aWindowGrouporDocumentGroup. Inside that builder you writeTextEditingCommands()to inject the Undo/Redo and Find items. TheTextEditorshown inTextEditingCommandsDemois the content those scene-level commands operate on once it has focus.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 thedraft-bound editor is active.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 ownCommandMenuin the samecommandsbuilder. 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.
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.
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()
}
}