Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I kill a rectangle of text in Emacs if the first and last lines are not long enough?

Tags:

emacs

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?

like image 447
Ryan C. Thompson Avatar asked Dec 07 '11 04:12

Ryan C. Thompson


People also ask

How do you kill multiple lines 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 .

How do I kill text in Emacs?

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.

How do I mark a rectangle in Emacs?

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 ).

What is the command to cut an entire line Emacs?

C-S-backspace ( kill-whole-line ) kills a whole line including its newline, regardless of the position of point within the line.

What does the rectangle kill command do in Emacs?

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.

What is the difference between Rectangle and column in Emacs?

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. […]

How do I delete text in a rectangle in Emacs?

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.

What is killing and yanking in Emacs?

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.


2 Answers

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.

like image 100
Tom Avatar answered Sep 20 '22 10:09

Tom


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 XXs at the beginning of the lines.

like image 32
Trey Jackson Avatar answered Sep 20 '22 10:09

Trey Jackson