Typing text in Emacs is straightforward. This lesson covers changing what is already there. C- means Ctrl and M- means Alt (Meta).
Deleting and killing
Backspacedeletes the character before the cursor.C-ddeletes the character at the cursor.M-dkills the word after the cursor.C-kkills from the cursor to the end of the line.
"Kill" is Emacs for cut. Killed text is saved in the kill ring, so you can get it back. Single characters removed with C-d are not saved there.
C-k stops before the line break. If you started at the beginning of the line, press it again to remove the now empty line.
Selecting a region
Press C-SPC (Ctrl and space) to set the mark, then move the cursor. The text between the mark and the cursor is the region.
C-wkills (cuts) the region.M-wcopies the region.
C-x h selects the whole buffer.
Yanking
C-y yanks (pastes) the most recently killed text. Straight after C-y, press M-y to swap it for an older entry in the kill ring. Press M-y again to go further back.
Note the clash with Vim: there, "yank" means copy. In Emacs it means paste.
Undo
Use C-/, C-_ or C-x u. Repeat to keep undoing. Which of these your terminal passes through can differ, and C-x u is a reliable choice.
To redo, Emacs 28 and later offer M-x undo-redo. On any version you can break the undo sequence, for example with C-f, and then undo again, which reverses your undos.
Search and replace
Press M-% (query-replace). Enter the old text, then the new:
Query replace: http
Query replace http with: httpsAt each match you answer:
yreplace this onenskip it!replace all the restqstop
Replacement starts at the cursor, so go to the top with M-< first if you want the whole buffer.
To replace everything from the cursor onwards without questions, use M-x replace-string.
Save your work
Edits exist only in the buffer until you press C-x C-s.
Try it
- Run
printf 'one http\ntwo http\nthree\n' > edit.txt, thenemacs -nw edit.txt. - On the first line press
C-ktwice, move down withC-n, and pressC-y. - Undo with
C-x uuntil the file looks as it did. - Press
M-<, thenM-%, replacehttpwithhttps, and answer!. - Save with
C-x C-s, exit withC-x C-c, and runcat edit.txt.
Check your understanding
Emacs Editing
All 5 sections ยท no timer
Unlimited retries
Questions from every section are combined into one assessment. Answer them all correctly to complete the article.
Your completion saves in this browser. Sign in to sync it to your account.