4

Here are two cases i am interested in:

Case 1: lines of identical length

Given some lines

const int name1
const int name2
const int name3
const int name4  

and some other lines

= 1
= 2
= 3
= 4

I would like to move the other lines to the end of the first lines.

const int name1 = 1
const int name2 = 2
const int name3 = 3
const int name4 = 4

Is there an easy way to do this?

Case 2: lines of different lengths

In this cae the lines of the first region do not have the same length:

const int someName
const int anotherName
const int yetAnotherName
const int name  

I would still like to move the other lines to the end of the first lines, like this:

const int someName = 1
const int anotherName = 2
const int yetAnotherName = 3
const int name = 4

Is there an easy way to do this?

  • Did you just roll back to the original question? :P Regardless, I have updated my solution; it now has solutions to rev 1 and rev 2 of your question. – Kaushal Modi Jan 16 '15 at 16:22
  • @kaushalmodi it seemed like a bad idea to edit the question so that the answer does not fit anymore. I will roll forward again. – Beginner Jan 16 '15 at 16:28
  • Can you also edit the question to reflect this? @nanny's solution applies to both revs. – Kaushal Modi Jan 16 '15 at 16:34
3

This function will take the thing you've most recently killed, appends the first line from that text at the end of the current line, then the second line at the end of the next line, etc.

(defun yank-at-end-of-lines ()
  "Yank the most recent kill to point.

If there are newlines in the most recent kill, append the first line
in the kill to the end of the current line, the second line in the kill
to the end of the next line, and so forth."
  (interactive)
  (dolist (cur-line-to-insert (split-string (current-kill 0)
                                            "\n"))
    (if (eobp)
        (newline)
      (move-end-of-line nil))
    (insert cur-line-to-insert)
    (forward-line)))

It does not use rectangles; you should kill the text (e.g., C-w), not kill the rectangle (e.g., C-x r k).

It also does not allow you to do anything but yank the most recent kill.

  • Great! If you want one that works from the most recent rectangle-killed thing instead of the previous killed thing, I can make that too. – zck Jan 16 '15 at 21:30
6

Solution

There is an easy solution to your updated question too, but you will need to install the Multiple Cursors package, also available through Melpa.

Step 1. Kill/cut the rectangle (same as before)

▮= 1
 = 2
 = 3
 = 4▮
  • Select the region shown above between the two points.
  • C-x r k - Kill/cut that rectangular region between the two points.

Step 2. Yank the just killed rectangle to the location of your choice

const int someName       ▮
const int anotherName
const int yetAnotherName
const int name
  • Bring the point at the above shown location
  • Note that this location needs to be at any point in the column that's not occupied by any of the below lines. If you can imagine a vertical line closest to the left which does not cut any of those 4 lines, bring the point to the right of that line.
  • C-x r y- Yank/paste the killed rectangle

That will result in the below

const int someName       = 1▮
const int anotherName    = 2
const int yetAnotherName = 3
const int name           = 4
  • Now we need to remove that extra spaces between the variable names the = signs.

Step 3. Removing extra space

For this step, first bring the point over the first = sign as shown below (the point is hiding the = sign beneath it).

const int someName       ▮ 1
const int anotherName    = 2
const int yetAnotherName = 3
const int name           = 4
  • Create Multiple Cursors at all = signs: M-3 C->. C-> is bound to mc/mark-next-like-this.

That will give you this; the = signs are still hidden beneath the cursors.

const int someName       ▮ 1
const int anotherName    ▮ 2
const int yetAnotherName ▮ 3
const int name           ▮ 4
  • C-SPC M-b M-f BKSPACE SPC RET - Delete any space between the multiple cursors and the words before them and instead insert a single space.

Result

const int someName = 1
const int anotherName = 2
const int yetAnotherName = 3
const int name = 4

Older answer

Using rectangle is the first thing that comes to my mind.

Step 1. Kill/cut the rectangle

▮= 1
 = 2
 = 3
 = 4▮
  • Select the region shown above between the two points.
  • C-x r k - Kill/cut that rectangular region between the two points.

Step 2. Yank the just killed rectangle to the location of your choice

const int name1 ▮
const int name2
const int name3
const int name4
  • Bring the point at the above shown location
  • C-x r y- Yank/paste the killed rectangle

Result

const int name1 = 1
const int name2 = 2
const int name3 = 3
const int name4 = 4

Learning

5

Third-party package answer:

Multiple Cursors

In additional to rectangle commands mentioned above, you can also do this using multiple cursors.

  1. Put a cursor on the beginnings of the bottom four lines. I used mc/mark-next-like-this three times (I map it to C->).

  2. C-k to kill the lines.

  3. Without exiting multiple-cursors, move your cursors to the end of the first four lines (C-p and C-e).

  4. SPC to add a space, C-y to yank in the killed lines.

  • I swear I tried kill and yank before using mc and it never worked; it always yanked the last kill at all cursors. But it works now. Great! :) – Kaushal Modi Jan 16 '15 at 16:27
1

I use this function for inserting cut text on multiple lines:

(defun yank-as-rectangle ()
  "Reinsert (\"paste\") the last stretch of killed text like a rectangle.
This splits the text into lines, then inserts each one like
`yank-rectangle' does.  Note that the lines may not be the same length."
  (interactive)
  (insert-rectangle (split-string (current-kill 0) "\r?\n")))

This is like a combination of yank and yank-rectangle. It inserts the text that yank would, but it does it like yank-rectangle would.

However, it doesn't handle your case where the lines you're yanking onto are not all the same length. I'd probably just yank at the position of the longest line, and then replace multiple spaces if necessary. (Normally, I'd just leave things aligned.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.