In Emacs, I sometimes want to kill or copy a rectangle of text such as this one:
XX first line
XX second line is longer
XX 3rd line short
I want to copy the three lines without the leading "XX" on every line. However, it is impossible to mark both corners of the required rectangle, because the first and third lines do not reach to the right edge of the rectangle that I'm after. So how would I copy the three lines above without the leading "XX" in emacs?
Move your cursor directly before the region you want to delete. Set a mark by pressing Ctrl-6 or Ctrl-^ . Move the cursor to the end of the region you want to delete and press Ctrl-k .
Kill rest of line or one or more lines ( kill-line ). The simplest kill command is C-k . If given at the beginning of a line, it kills all the text on the line, leaving it blank.
A rectangular region can also be marked using the mouse: click and drag C-M-mouse-1 from one corner of the rectangle to the opposite. Kill the text of the region-rectangle, saving its contents as the last killed rectangle ( kill-rectangle ).
C-S-backspace ( kill-whole-line ) kills a whole line including its newline, regardless of the position of point within the line.
This kills the rows of the column (s) you’re interested in. To move the columns you want to an external application like LibreOffice, Gnumeric or back to the Web browser, you’ll need to kill the text in Emacs so it is in the clipboard – it won’t make it there with the rectangle kill command.
A character in a line is a “column”. Rectangles are edits on portions of the region between two buffer columns. The EmacsManual reads, The rectangle commands operate on rectangular areas of the text: all the characters between a certain pair of columns, in a certain range of lines. […]
This is new in emacs 24.4 [see Emacs 24.4 Features (released 2014-10) ] Alt + x rectangle-mark-mode, then move cursor to highlight a rectangular area. Press Delete ⌦ will delete text in that rectangle area.
Vocabulary: In Emacs, we call killing what others call cutting, and yanking what others call pasting. killing moves text from the document to the top of the kill ring. We call “saving onto the kill-ring” what others call copying text. The kill-ring is similar to a clipboard: it is a list of the last 60 killed items, from which items can be yanked.
Start marking the rectangle from the first line and when you are at the end of the 3d line then just type spaces until the line is long enough. That's what I usually do.
After copying you can type M-\
(delete-horizontal-space
) to remove the added spaces.
I see two options that jump out.
The first is to use M-x artist-mode, this will let you move to the position on the first/last line you want. At which point you cut the rectangle C-x r k, and then paste it wherever you want. Don't forget to type C-c C-c to exit artist mode.
The second is to just cut all three lines using the regular C-w
and then when you paste, delete the rectangle of XX
s at the beginning of the lines.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With