How to add or remove text at the end of many lines at once in Emacs?

eg. I want to add a double quote to the end of a bunch of selected lines. Or I want to remove two characters from a group of selected lines. Is there a team for this? I know that there is “Mx r t” for inserting a rectangle of text, but this only works if everything is vertically aligned, which usually does not apply to line ends.

+5
source share
3 answers

You can

  • save keyboard macro C-x ( C-e " C-f C-x ), respectively C-x ( C-e Backspace Backspace C-f C-x ). Then call the macro with the C-x e, e, e, e...
  • M-C-%. $ " , ..$ , .
+7

M-x replace-regexp $ " , , replace-regexp.

+5

Here's a clearer way to see the results in each row when entering them on one line.

Take the latest copy of yasnippet from http://github.com/capitaomorte/yasnippet and add to your.emacs

(require 'yasnippet)

(defun yas/add-to-end-of-lines-snippet ()
  (interactive)
  (when (region-active-p)
    (let ((snippet (replace-regexp-in-string "$" "$1" (buffer-substring (region-beginning) (region-end)))))
      (delete-region (region-beginning) (region-end))
      (yas/expand-snippet snippet))))

Now select a region and enter M-x add-to-end-of-lines-snippet.

+3
source

All Articles