In Emacs, edit multiple lines at once

I believe that textmate has a mode in which if you start typing, the same will be entered in all the lines you select. Is there something similar in emacs? I guess the rectangles method might help me, but I'm not sure how ...

+63
emacs
Apr 17 '09 at 18:46
source share
8 answers
+87
Apr 17 '09 at 18:54
source share

You absolutely must try to set some cursors:

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

In marmalade and melpe, it’s so simple:

 Mx package-install multiple-cursors
+52
Feb 25 '13 at 17:37
source share

One solution is to use CUA mode. Activate cua mode with Mx cua-mode , select the beginning of the rectangle: first press C-Enter , then move the cursor with the standard move commands to make a selection, now pressing the button at any time will cursor over the corners of the rectangle, which allows you to add or add text to the selection.

+22
Apr 17 '09 at 19:19
source share

To do this, you can use the following commands (and keys):

  • open-rectangle (Cx, r, o) add spaces
  • kill-rectangle (Cx, r, k) delete
  • clear-rectangle (Cx, r, c) replace with spaces
  • Mx string-insert-rectangle fill with the given text

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

+19
Apr 17 '09 at 18:56
source share

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

Recently, I had to output an SQL query to a file, and then format it in a MYSQL INSERT query. This is how Emacs made my life easier.

The file looks like this:

 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 it to look 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'), 
  • Place the cursor on the first line
  • Press Cx ( to start recording the macro [At this point, all your key inputs are recorded so carefully follow the instructions]
  • Press Ca to go to the beginning of the line
  • Type "(", then Mf to move the word, and then type ","
  • Cn to go to the next line and then Cx ) to complete the macro
  • Cu 11 Cx e repeat the macro n (11 in this case) times

Eureka! Now, if you have not failed, you will get something similar to 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 moment, I am going to leave you to find out the rest. But, before I go, I would like to mention that there are many ways to achieve this kind of thing. This is just one of these ways, and it happens to be my favorite way.

I hope you found this useful;)

+16
Feb 12 '14 at 10:40
source share

I believe that you are looking for the cua mode proposed by Boscom. http://www.vimeo.com/1168225?pg=embed&sec=1168225 this screencast can give you an idea of ​​how to use this.

+4
Apr 17 '09 at 19:28
source share

Rectangles are simple things, such as removing the same number of spaces in adjacent lines.

Otherwise, keyboard macros are the way to go.

+3
Apr 17 '09 at 18:50
source share

The answers above are for inserting text into columns. TextMate "Edit each line in selection" inserts the same text in each line, regardless of the length of each line. I am learning Lisp now, since the exercise I wrote a function for 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 )))) 

First you make a selection that includes all the lines you want to affect, and then run the function using Mx append-to-lines.

+2
Apr 21 '09 at 22:33
source share



All Articles