Next 17.D: Buffers Up 17: Emacs Prev 17.B: Changes Contents


Specialized

§ 17.C: Cutting and Pasting in emacs


Emacs lets you be destructive to text by either deleting or killing it. The difference is that killed things can be brought back (yanked), while deleted things are gone forever (well, maybe nothing is forever, emacs has probably made backup copies of your file on the disk).

The killed text from a file is stored in a common area, the kill ring, so you the killed text from one file can be yanked into another file. In this respect, kill and yank are similar to cut and paste on a PC.

When you put something into the kill ring, it does not delete the prior entry. So when you yank it back, the prior item will be waiting to be brought back next. This is a somewhat different from the usual cut and paste. So, if you cut some text and try to paste it in several places, the second and suceeding yanks will give you different text! This is great, if it is what you want, but real interesting otherwise.

If you wish to cut text and then paste it to several places, you must use the copy command.

Because emacs can be used without a mouse, there are several commands to allow you to kill different quantities of text. A few basic kill commands are:

    M-[Del]   kill the word immediately before the cursor
    M-d       kill the word after the cursor
    C-k       kill from the cursor position to the end of line

To bring back killed text, use the yank command C-y.

As long as you keep repeating the kill command, all text deleted will be considered to be from the same kill and will be stored as one unit. So, if you kill three lines in a row, a single yank will bring all three lines back.

To kill a large region of text, mark the location at which you want to begin the kill with the mark command, C-@ or C-[space bar], and move your cursor to the end of the killed region (world) and mark it wth C-w.

If you want to copy a region of text without deleting it, mark the beginning of the region as before, but instead of using the C-w command to delete the world, use the M-w command instead to copy the world. This is safer, especially until you become more familiar with Unix.

If you have gone on a killing spree and deleted a whole bunch of things, you may want to to retrieve an earlier kill, but not a latter one. If so, the M-y command scrolls though the kill ring until you find the kill you are looking for. If you follow your yank command C-y with the M-y command, you will get the second to last thing killed. If you continue to use the command M-y, you will scroll through all of your previous kills. Keep in mind that wherever you stop is reset as the most recent kill.

If you accidentally kill something, you have two ways to repent. You can use the undo command, C-x u which undoes whatever command you did last. Alternatively, if you have not continued on your killing spree, you can yank your text back with C-y.

Try some random killing and yanking before you start the next section.


Next 17.D: Buffers Up 17: emacs Prev 17.B: Changes Contents