in Emacs, edit multiple lines at once

前端 未结 8 1989
天涯浪人
天涯浪人 2020-12-22 16:29

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

8条回答
  •  一生所求
    2020-12-22 17:00

    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.

提交回复
热议问题