in Emacs, edit multiple lines at once

Emacs

Emacs Problem Overview


I believe textmate has a mode where if you start typing, the same thing will be entered on all the lines you've selected. Is there something similar to this in emacs? I'm guessing there's a way rectangles can help me, but I'm not sure how...

Emacs Solutions


Solution 1 - Emacs

It's as simple as this: C-x r t

Some examples are here: http://ergoemacs.org/emacs/emacs_string-rectangle_ascii-art.html

Solution 2 - Emacs

You absolutely need to try installing multiple cursors:

https://github.com/magnars/multiple-cursors.el

It's in marmalade and melpa so just:

M-x package-install multiple-cursors

Solution 3 - Emacs

One of the solutions is using CUA mode. Activate cua mode with M-x cua-mode, select rectangle begin: first press C-Enter then move cursor with standard movement commands to make selection, now pressing enter at any time will cycle cursor through corners of the rectangle enabling you to prepend or append text to the selection.

Solution 4 - Emacs

You can use the following commands (and keys) to accomplish this:

  • open-rectangle (C-x, r, o) add spaces
  • kill-rectangle (C-x, r, k) delete
  • clear-rectangle (C-x, r, c) replace with spaces
  • M-x string-insert-rectangle fill with specified text

Here is a complete description of those features: http://www.gnu.org/software/emacs/manual/html_node/emacs/Rectangles.html

Solution 5 - Emacs

For those of you that want to do this for more complicated scenarios and want to do it without installing a new module please read on. (This is possible in Emacs without installing MarkMultiple, although I personally use and love MarkMultiple)

I recently had to OUTPUT a SQL query to a file and then format it into a MYSQL INSERT query. Here is how Emacs made my life easy....

File looks like:

1   I am a random text
2	I am not
3	G, you've gone mad
4	Click on this link
5   Transfer in progress (we've started the transfer process)
6	But transfer happend yesterday
7	No you are
8	Oh please! this is getting too much!
9	I love emacs
10	I cant be bothered with	this any more
11	its time to raise the bar
12	show me	how to expand my territory

And I want to make it look like:

(1,   ,'I am a random text'),
(2,   ,'I am not'),
(3,   ,'G, youve gone mad'),
(4,   ,'Click on this link'),
(5,   ,'Transfer in progress (weve started the transfer process)'),
(6,   ,'But transfer happend yesterday'),
(7,   ,'No you are'),
(8,   ,'Oh please! this is getting too much!'),
(9,   ,'I love emacs'),
(10,  ,'I cant be bothered with this any more'),
(11,  ,'its time to raise the bar'),
(12,  ,'show me how to expand my territory'),
  1. Place cursor at first line
  2. Press C-x ( to start recording macro [At this point all your key inputs are being recorded so please follow the instructions carefully]
  3. Press C-a to go to the beginning of the line
  4. Type "(" followed by M-f to move forward a word and then type ","
  5. C-n to go to the next line, followed by C-x ) to end the macro
  6. C-u 11 C-x e repeat the macro n (11 in this case) times

Eureka! By now if you have not failed you will get something that looks like this:

(1,   I am a random text
(2,   I am not
(3,   G, youve gone mad
(4,   Click on this link
(5,   Transfer in progress (weve started the transfer process)
(6,   But transfer happend yesterday
(7,   No you are
(8,   Oh please! this is getting too much!
(9,   I love emacs
(10,  I cant be bothered with this any more
(11,  its time to raise the bar
(12,  show me how to expand my territory

At this point I am going to leave you to figure out the rest. But, before I go I like to mention that there are quite a few ways of achieving this sort of thing. This is just one of those ways and it happens to be my favourite way.

Hope you found it helpful ;)

Solution 6 - Emacs

Rectangles are for simple stuff like deleting the same amount of spaces in adjacent lines.

Otherwise keyboard macros are the way to go.

Solution 7 - Emacs

I believe you are looking for the cua-mode that was suggested by boskom. http://www.vimeo.com/1168225?pg=embed&sec=1168225 this screencast might give you an idea of how to use this.

Solution 8 - Emacs

The answers show above are for inserting text in columns. TextMate's "Edit Each Line in Selection" inserts the same text in each line regardless of the length of each lines. I'm learning Lisp now, so as an exercise I wrote a function to do this:

(defun append-to-lines (text-to-be-inserted)
  ;;Appends text to each line in region
  (interactive "sEnter text to append: ")
  (save-excursion
    (let (point-ln mark-ln initial-ln final-ln count)
      (barf-if-buffer-read-only)
      (setq point-ln (line-number-at-pos))
      (exchange-point-and-mark)
      (setq mark-ln (line-number-at-pos))
      (if (< point-ln mark-ln)
          (progn (setq initial-ln point-ln final-ln mark-ln)
                 (exchange-point-and-mark))
        (setq initial-ln mark-ln final-ln point-ln))
      (setq count initial-ln)
      (while (<= count final-ln)
        (progn (move-end-of-line 1)
               (insert text-to-be-inserted)
               (next-line)
               (setq count (1+ count))))
      (message "From line %d to line %d." initial-ln final-ln ))))

You first make a selection that includes all the lines you want to affect and then run the function with M-x append-to-lines.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionallyourcodeView Question on Stackoverflow
Solution 1 - EmacsallyourcodeView Answer on Stackoverflow
Solution 2 - Emacsnic ferrierView Answer on Stackoverflow
Solution 3 - EmacsboskomView Answer on Stackoverflow
Solution 4 - EmacspaweloqueView Answer on Stackoverflow
Solution 5 - EmacsHamsterView Answer on Stackoverflow
Solution 6 - EmacsstarblueView Answer on Stackoverflow
Solution 7 - EmacsAmjithView Answer on Stackoverflow
Solution 8 - EmacsgdelfinoView Answer on Stackoverflow