Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 4: Basic Editing

Previous HourNext Hour

Sections in this Hour:

 

Cut, Copy, and Paste


A capability that makes a computer better than an ordinary typewriter is cut-and-paste, which is the capability to move text around in your documents, copy text into several places, and so on.

To mark a region you want to cut or copy, you can use either the mouse or the keyboard.

Marking Text for Cut-and-Paste Using the Mouse

This task shows you how to mark a region using the mouse and copy this region to the clipboard (called kill-ring in Emacs). Follow these steps:

1. Place the mouse over the start of the text you want to mark.

2. Click the left mouse button and, while you keep the mouse button down, drag to the location where the selection should end.

3. Release the mouse button.

4. If you want to copy the text to the clipboard (that is, don't delete it from the buffer), select the Edit menu, and chose Copy. If, on the other hand, you want to cut the text to the clipboard (that is, delete it from the buffer), go to the Edit menu and select Cut.

If you need to copy the text, it is not necessary in GNU Emacs to select the copy entry from the menu.

Marking Text for Cut-and-Paste Using the Keyboard

This task shows you how to mark a region using the keyboard and copy this region to the clipboard (called kill-ring in Emacs).

1. Place point at the start of the text you want to select.

2. Press Ctrl+Spacebar. Below the buffer, the text Mark set should appear.

3. Go to the location where you want the selection to end.

4. If you want to copy the text to the clipboard, press M-w (kill-ring-save); otherwise press C-w (kill-region) to cut the text to the clipboard.

It is often useful to mark the region using the mouse and then cut or copy using the keyboard. That is, use steps 1-3 from the previous task and step 4 from this task.

Although it might seem difficult to remember the keybindings, you should try to do so. They make your interaction with Emacs much faster, for example, if you do not need to use the mouse when you use cut-and-paste.

You can paste using either the mouse or the keyboard. To paste using the mouse simply press the middle mouse button at the location where you want to insert the text. To paste using the keyboard, place point at the location where you want the text to be inserted and press C-y (yank).

Emacs saves not only the latest selection made but several of them. This makes it possible to paste an old selection into the buffer. To paste an old selection, press C-y (yank), which pastes the latest selection made into the buffer. Next, press M-y (yank-pop), which replaces the inserted text with the second-to-last selection. Subsequent M-y's replace the text with even older selections.

Pasting the Second-to-Last Selection into the Buffer

The following steps show you how to paste an earlier selection into the buffer as described previously.

1. Before I created Figure 4.7 I cut the text One, Two, and Three in that order (that is, Three is the latest selection, Two is the previous, and One is the first). The initial content of the buffer is shown in Figure 4.7.

Figure 4.7
Layout before C-y was pressed the first time.

2. Now press C-y (yank). This inserts the latest selection (see Figure 4.8).

Figure 4.8
Now C-y is pressed. The latest kill is pasted in.

3. Press M-y once to replace the pasted text with the second-to-last selection (see Figure 4.9).

Figure 4.9
After C-y, M-y is pressed, pasting in the second-to-last kill.

4. Pressing M-y once again replaces the pasted text again, this time with the third-to-last selection (see Figure 4.10).

Figure 4.10
Finally, pressing M-y once again paste the third-to-last to kill.

If you press any other key between C-y and any of the M-y's then you are stuck with the given selection. That is, you won't be able to press M-y to get to older selections.

If you are using GNU Emacs you might also find the older selections in the menu bar. They are located in the Edit menu, under the submenu Select and Paste. Please note that this is not available in XEmacs.

Other Copying Commands

Apart from the command bound to C-w (kill-region), other commands delete text from the buffer and copy it to the clipboard. The most useful of these is C-k (kill-line), which removes from point to the end of the line and inserts it on the clipboard. You can press it several times to remove several lines and append them to the text on the clipboard. That is, when you paste the text, all the lines are pasted as one entity.

Remember from earlier in the hour that C-d deletes the next character. Likewise the Delete key deletes the previous character. Two other very useful commands cut text-- M-d (kill-word) deletes the next word and M-DEL (backward-kill-word) kills the previous word. Both these commands cut text to the clipboard.

An other way to insert some text into your document is to simply insert a whole file. You can do that by pressing C-x i (insert-file).

Windows Notes - GNU Emacs interacts with the Windows Clipboard similarly. The top item in the kill-ring is inserted into the Clipboard, ready for you to paste into another application.

If you go to another application and copy some text into the Clipboard, you can copy it into GNU Emacs with C-y. Your previous Emacs kill is still in the kill-ring, and you can still get at it with M-y. Data entered into the Clipboard from other applications affect the GNU Emacs kill-ring only if you paste from the Clipboard into Emacs.


Sams Teach Yourself Emacs in 24 Hours

ContentsIndex

Hour 4: Basic Editing

Previous HourNext Hour

Sections in this Hour: