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

In Emacs, I sometimes want to kill or copy a rectangle of text like this one:

XX first line XX second line is longer XX 3rd line short 

I want to copy three lines without the leading "XX" in each line. However, it is impossible to mark both corners of the required rectangle, because the first and third lines do not reach the right edge of the rectangle that I need. So, how would I copy the three lines above without the leading "XX" in emacs?

+6
source share
4 answers

Start labeling the rectangle with the first line and when you are at the end of the three-dimensional line, just enter spaces until the line is long enough. This is what I usually do.

After copying, you can enter M-\ ( delete-horizontal-space ) to remove the added spaces.

+5
source

I see two options that jump out.

Firstly, use Mx artist-mode , this will allow you to jump to the position on the first / last line you want. At this point, you cut the rectangle Cx rk , and then paste it anywhere. Remember to enter Cc Cc to exit artist mode .

Secondly, just cut all three lines using regular Cw and then when you paste, remove the XX rectangle at the beginning of the lines.

+4
source

You can look at cua-selection-mode (Mx cua-selection-mode).

It allows you to do what you want, but only in graphical emacs (and not in the terminal). Key bindings are slightly different: you start the area using C-RET (instead of C-SPC) and cut / set the rectangle simply using Cw / Cy (without using the rectangle command).

You can find an example on this blog .

To enable the mode forever, just add

 (cua-selection-mode t) 

to your .emacs.

PS full cua-mode will also change further key bindings. No need to use it.

+3
source

simple modifications to non-unitary defuns. kill-right-rectangle function does what you want. I am sure there is a much better way to do this. However, it works.

Two points relative to the following code.
- Since the idea is to bring the text to the end of the line, you don’t even need to specify the last column. just mark on the first line (in your example, specify "f") and go to the third line to call the "3" function. (we only need one column, so no need to move the cursor to the right)
- this does not work if the buffer is read-only. its just not implemented.

 (defun end-column (POINT) "returns end column" (save-excursion (goto-char POINT) (move-end-of-line 1) (current-column))) (defun apply-on-right-rectangle (function start end &rest args) (let (startcol startpt endcol endpt final-point) (save-excursion (goto-char start) (setq startcol (current-column)) (beginning-of-line) (setq startpt (point)) (goto-char end) (setq endcol (current-column)) (forward-line 1) (setq endpt (point-marker)) ;; ensure the start column is the left one. (if (< endcol startcol) (let ((col startcol)) (setq startcol endcol endcol col))) ;; start looping over lines (goto-char startpt) (while (< (point) endpt) (setq endcol (end-column (point))) (apply function startcol endcol args) (setq final-point (point)) (forward-line 1))) final-point)) (defun delete-extract-right-rectangle (start end &optional fill) (let ((lines (list nil))) (apply-on-right-rectangle 'delete-extract-rectangle-line start end lines fill) (nreverse (cdr lines)))) (defun kill-right-rectangle (start end &optional fill) (interactive "r\nP") (condition-case nil (setq killed-rectangle (delete-extract-right-rectangle start end fill)) ((buffer-read-only text-read-only) (setq killed-rectangle (extract-rectangle start end)) (if kill-read-only-ok (progn (message "Read only text copied to kill ring") nil) (barf-if-buffer-read-only) (signal 'text-read-only (list (current-buffer))))))) 
0
source

Source: https://habr.com/ru/post/903248/


All Articles