# Commands and transactions

Use commands first, then understand how changes share document history.

Commands expose actions such as Bold, insertion and undo. Toolbars, shortcuts and application code use them to modify the same document.

## Run one command

With Bold or StarterKit installed:

```ts [Toggle Bold immediately]
editor.commands.toggleBold()
```

The action targets the current selection. Its boolean result reports handling, not persistence.

## Combine actions

After clicking a toolbar, return focus before formatting:

```ts [Chain commands]
editor.chain().focus().toggleBold().run()
```

Actions share a transaction context, submitted by `run()`. A transaction describes content and selection changes that history uses for undo and redo.

`run()` reports whether every action returned true. It does not automatically roll back: one false result does not remove steps already added by other actions. Check with the equivalent `can().chain()` when needed, and make custom commands reject before modifying `tr`.

## Check whether an action can run

```ts [Check without changing content]
const enabled = editor.isEditable && editor.can().toggleBold()
```

`can()` checks the current state without committing changes. It does not check application write permissions and may return true while read-only. Controls typically combine `editor.isEditable && editor.can().toggleBold()`; use `isActive('bold')` to query active formatting separately.

`setEditable(false)` controls View input; business commands and collaboration transactions can still write. Buttons, AI, asynchronous services and automation must validate their own permissions, targets and current editability immediately before invoking commands. Hiding controls is insufficient.

## Write commands

Register actions in `addCommands()` and use its supplied `commands`, `tr` and `dispatch`. Direct calls, chains and `can()` then share the implementation. Checks must not start requests, open dialogs or mutate external state.

See [toolbar integration](/en/build/commands) and the [content-extension tutorial](/en/build/extension).
