tags:

views:

737

answers:

6

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

A: 

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

Otherwise keyboard macros are the way to go.

starblue
+9  A: 

Blarg! It's simple: use C-x r t

allyourcode
Chad, thanks for the correction. Now a really feel dumb ;).
allyourcode
+4  A: 

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

lewap
+6  A: 

One of the solutions is using CUA mode. Activate cua mode with M-x cua-mode, select rectangle regin: 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.

boskom
Thanks, boskom. I'm using version 21.3.1, so this mode doesn't seem to be installed. I probably won't install it, because I'm finding that I like C-x r t.
allyourcode
+3  A: 

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.

jinxed_coder
Thank you for the link! A picture says a thousand words :).
allyourcode
+1  A: 

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.

gdelfino
I haven't tested this out, but nice work. I was more interested in the "add the same text to the same column" approach, but I'm sure this would come in handy as well.
allyourcode