Sub topic 11
Control-key text shortcuts from the Emacs tradition
This chapter assumes you know the modifier symbols (⌘, ⌥, ⌃, ⇧) and the core editing and navigation shortcuts from earlier chapters. You should also be comfortable typing in a text field in an app such as Notes or Mail.
Where these shortcuts come from
Emacs is a text editor from the 1970s. Its authors bound editing commands to the Control key plus a letter, and chose letters that hint at the action: F for forward, B for backward, N for next line, P for previous line, A for the start of a line (the first letter of the alphabet), E for the end.
Mac text controls come from NeXTSTEP and Cocoa. They adopted a subset of these bindings, and the subset is still present in macOS today. They work in most places where you can type text: Notes, Mail, TextEdit, Messages, Pages, Safari's address bar, and many text fields in third-party apps.
The main benefit is that your hands stay on the home row. Arrow keys and ⌘-arrow combinations make you reach for the corner of the keyboard. The Control shortcuts sit under your fingers while you type.
The core set
| Shortcut | Action | Mnemonic |
|---|---|---|
| ⌃F | Move forward one character | **F**orward |
| ⌃B | Move back one character | **B**ackward |
| ⌃N | Move down one line | **N**ext |
| ⌃P | Move up one line | **P**revious |
| ⌃A | Move to the start of the line or paragraph | first letter |
| ⌃E | Move to the end of the line or paragraph | **E**nd |
| ⌃D | Delete the character to the right of the cursor | **D**elete |
| ⌃H | Delete the character to the left (same as Delete) | **H**ack back, also ASCII backspace |
| ⌃K | Delete from the cursor to the end of the line | **K**ill |
| ⌃Y | Paste the text last removed with ⌃K | **Y**ank |
| ⌃T | Swap the characters on either side of the cursor | **T**ranspose |
| ⌃O | Insert a line break after the cursor without moving the cursor | **O**pen line |
| ⌃L | Scroll so the cursor line is in the middle of the view | **L**ocate or **L**ook |
| ⌃V | Move down one page | similar to Page Down |
Most of these are the ones you will use daily. ⌃A, ⌃E, ⌃K, ⌃Y, ⌃D and ⌃T pay off first. ⌃F, ⌃B, ⌃N and ⌃P are slower than word-level movement with ⌥, so treat them as small adjustments.
Moving without leaving the home row
Suppose you are typing a long sentence and spot a typo near the start of the line. A typical sequence with Control keys is:
- Press ⌃A to jump to the start of the line.
- Press ⌥→ a few times to reach the wrong word.
- Fix it, then press ⌃E to return to the end and keep typing.
⌃A and ⌃E combine well with the movement shortcuts from the earlier chapter. Use ⌥ for the word-level hops and ⌃ for the line ends.
⌃A versus ⌘←
These two look identical in a short line, but they differ in wrapped text. ⌘← moves to the start of the visual line, the row you see on screen. ⌃A moves to the start of the paragraph, which is the text up to the previous hard line break. In a long wrapped paragraph, ⌃A can jump several rows up. The same difference applies to ⌃E and ⌘→.
This difference is usually a small effect, but it matters when you use ⌃K, covered next.
Kill and yank
Emacs uses the word "kill" for cut and "yank" for paste, and macOS has kept both.
- ⌃K deletes from the cursor to the end of the paragraph.
- ⌃Y inserts the text you last removed with ⌃K.
The killed text is stored in a private buffer, separate from the clipboard. This has two practical consequences:
- You can use ⌘C and ⌃K independently. Copy a URL with ⌘C, delete the rest of a line with ⌃K, paste the URL with ⌘V, and then use ⌃Y to bring back the deleted text. Neither action overwrites the other.
- ⌃K does not put anything on the clipboard. If you press ⌃K and then switch apps and press ⌘V, you paste whatever you copied earlier, not what you killed.
If the cursor is already at the end of a line, ⌃K deletes the line break, which pulls the next line up. That is useful for joining two lines. It can surprise you if you expected nothing to happen.
Here is a worked example. The cursor is after "Dear" in this line:
Dear Sam, thanks for the update on the budget review.Press ⌃K and the line becomes Dear. Move to another line, press ⌃Y, and Sam, thanks for the update on the budget review. appears at the new cursor position. This works as a quick way to move the tail of a sentence.
Small fixes: ⌃T, ⌃D, ⌃H and ⌃O
- ⌃T swaps the character before the cursor with the one after it. If you typed
tehand the cursor sits betweeneandh, ⌃T turns it intothe. At the end of a line it swaps the last two characters, which fixes the common case of a transposition at the end of a word. - ⌃D deletes forward. The Delete key on a Mac laptop deletes backward, and the Fn-Delete combination deletes forward. ⌃D is a one-handed alternative.
- ⌃H deletes backward, exactly like the Delete key. It is useful when your right hand is away from the keyboard.
- ⌃O adds a new line after the cursor but leaves the cursor where it is. After ⌃O, the text to the right of the cursor sits on the next line, and you can keep typing on the current line.
Where they do not work, and where they differ
Not every app follows the system text rules.
- Cocoa text fields (Notes, Mail, TextEdit, Safari's address bar) support the full set above.
- Browsers and Electron apps often support a subset inside text fields. ⌃A, ⌃E, ⌃K and ⌃Y usually work, but ⌃O and ⌃L may do something else or nothing.
- Code editors may remap them. Some editors give ⌃K a different meaning, such as a first key of a two-key chord.
- Terminal does not use the system text bindings. The shell, through a library called readline, provides its own versions.
The Terminal differences deserve a table, because the same keys behave in a similar but not identical way.
| Shortcut | In a Cocoa text field | At a Terminal prompt |
|---|---|---|
| ⌃A | Start of paragraph | Start of command line |
| ⌃E | End of paragraph | End of command line |
| ⌃K | Kill to end of paragraph | Kill to end of command line |
| ⌃Y | Yank last killed text | Yank last killed text |
| ⌃U | Not bound by default | Kill from cursor to start of line |
| ⌃W | Not bound by default | Kill the previous word |
| ⌃D | Delete forward | Delete forward, or end the session on an empty line |
The last row is a trap. In a shell, pressing ⌃D on an empty prompt sends end-of-input, which closes the session. Do not press it out of habit while a Terminal window holds work you care about.
Customizing the bindings
macOS reads text key bindings from a file named DefaultKeyBinding.dict. You can add your own at ~/Library/KeyBindings/DefaultKeyBinding.dict. The KeyBindings folder does not exist by default, so create it first.
mkdir -p ~/Library/KeyBindings
cat > ~/Library/KeyBindings/DefaultKeyBinding.dict <<'EOF'
{
"^u" = "deleteToBeginningOfLine:";
}
EOFThis binds ⌃U to delete from the cursor to the start of the line. It matches the shell behavior from the table above, and it is the mirror image of ⌃K.
A few rules apply:
- Quit and reopen each app before testing. Apps read the file at launch.
- The file uses an old property-list syntax. Each entry ends with a semicolon, and a missing one makes the whole file fail to load.
- In key names,
^means Control,~means Option,$means Shift and@means Command. - Apps that draw their own text controls, and some Electron apps, ignore the file.
Common errors
- Expecting ⌃K to fill the clipboard. Use ⌃Y, not ⌘V, to paste killed text.
- Using ⌃A and ⌃E in wrapped text and landing in the wrong place. They work on paragraphs. Use ⌘← and ⌘→ when you want the visual line.
- Pressing ⌃D in Terminal. It can end the session.
- Assuming one app's behavior holds everywhere. Test a shortcut in a new app before you rely on it with unsaved text.
- Editing
DefaultKeyBinding.dictand not restarting apps. The change will look like it did nothing.
A short practice plan
Pick three shortcuts, not fourteen. A good first set is ⌃A, ⌃E and ⌃K. Use them for a day, and when you reach for the arrow keys or the mouse, stop and use the Control version. When those three are automatic, add ⌃Y and ⌃T.
Exercise
You are in Notes with the cursor in the middle of a long wrapped paragraph. You press ⌃K, move to a new note, and press ⌘V. Nothing from the paragraph appears, though earlier you copied a phone number with ⌘C. What was pasted, and which shortcut should you have used to bring back the killed text?
Show answer
The phone number was pasted, because ⌃K stores text in a separate kill buffer and not on the clipboard. ⌘V pastes the clipboard. To insert the killed text, press ⌃Y.
Exercise
You typed recieve and the cursor sits between the i and the e. Which Control shortcut fixes the spelling, and what does ⌃A do differently from ⌘← in a wrapped paragraph?
Show answer
Press ⌃T to swap i and e, giving receive. In a wrapped paragraph, ⌃A moves to the start of the whole paragraph, while ⌘← moves only to the start of the visual line the cursor is on.