ARCHIVED: In Emacs, how do I cut, paste, and copy a block of text?
Note: In Emacs, cutting and pasting have different names. Cutting is called killing, and pasting is called yanking, as in yanking the text back from the clipboard and into the file. In this document, the terms "cut" and "paste" will be used, but bear in mind that all other Emacs documentation will use "kill" and "yank" instead.
To cut or copy text, first you must select it. (See ARCHIVED: In Emacs, how do I select a region of text?) The default way to cut and paste text in Emacs is to use keyboard commands, but there are easier ways available if you are using Emacs in an X Window session. X Window methods are presented later in this document.
Once you have a region selected, the most basic commands are:
- To cut the text, press
C-w
. - To copy the text, press
M-w
. - To paste the text, press
C-y
.
You also can use commands for removing chunks of text without first selecting them (e.g., deleting the next character, the next word, or from the cursor to the end of the line). For a listing of these, see ARCHIVED: GNU Emacs Quick Reference Guide.
If you wish to cut or paste a rectangular area of text, rather than a continuous area that wraps from margin to margin, see ARCHIVED: How do I use Emacs to cut and paste a rectangular region (columns or fields) of text?
By default, when you paste into the buffer, what gets
pasted is the very last thing that you cut or copied. However,
everything you have cut or copied is stored in the kill ring (which is
analogous to a clipboard), and can be retrieved when you paste. Thus,
you can paste earlier sections of cut or copied text. To do so,
immediately after having pasted with C-y
, press
M-y
. The text that was just pasted will be
replaced by an earlier section of text you selected. You may press
M-y
multiple times to cycle backwards through all of your
previously cut or copied text.
To undo a given action, you can use the undo
command,
which you can activate in one of three ways: pressing C-_
(Ctrl
-underscore); pressing
C-x u
; or entering
M-x undo
. You may repeat the undo
command
several times, up to an internal limit. (Usually several thousands of
characters worth of changes are stored and available for being undone.)
Using the mouse in the X Window System
If you are running Emacs in its own window (and not within an "xterm" window) as part of an X Window session, you can select the region simply by using the mouse. Click the mouse's left button at the start of the area to be selected, and drag the mouse to the end of the region. The area you selected should be highlighted.
In the X Window System, the text is automatically copied when it is
highlighted. To paste this text, move the cursor to or click the mouse
at the location in which you wish to paste this text, and single-click
the middle button on the mouse. If you do not have a middle button on
your mouse, try clicking both the left and right buttons
simultaneously. Otherwise, you will have to use the keyboard commands
for pasting text, as described above. Also note that these commands
will not work correctly if you are running Emacs within an xterm
or console window.
You cannot cut text directly with the mouse in the X Window System.
At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.
Related documents
This is document aiug in the Knowledge Base.
Last modified on 2018-01-18 13:12:57.