Emacs复制匹配行

Sin*_*ned 15 emacs

在Emacs中,如何轻松复制与特定正则表达式匹配的所有行?最好在我输入时突出显示匹配的行.

occur 通过将它们复制到缓冲区来获得中途,但它增加了许多额外的东西.

phi*_*ils 28

从Emacs 24开始,occur确实提供了一个简单的解决方案:

C-uM-so .*pattern.* RET

当您C-u自己使用作为前缀参数时,每行的匹配部分将插入*Occur*缓冲区,而不包含所有正常的装饰.

请注意,因为只有相匹配的正则表达式行的一部分用于(不像一个正常的发生),你需要的前端和后端.*,以确保您捕捉到整条生产线.

occur处理参数的细节有点棘手,所以C-hf occur RET如果你想了解更多,请仔细阅读.


Tre*_*son 12

这个怎么样:

(defun copy-lines-matching-re (re)
  "find all lines matching the regexp RE in the current buffer
putting the matching lines in a buffer named *matching*"
  (interactive "sRegexp to match: ")
  (let ((result-buffer (get-buffer-create "*matching*")))
    (with-current-buffer result-buffer 
      (erase-buffer))
    (save-match-data 
      (save-excursion
        (goto-char (point-min))
        (while (re-search-forward re nil t)
          (princ (buffer-substring-no-properties (line-beginning-position) 
                                                 (line-beginning-position 2))
                 result-buffer))))
    (pop-to-buffer result-buffer)))
Run Code Online (Sandbox Code Playgroud)

  • 嗯……这是一种常见的做法。这里真的只有三件事发生。1) 创建缓冲区,2) 搜索当前缓冲区,3) 在新缓冲区中插入搜索结果... (2认同)

sco*_*zer 9

您可以使用keep-lines获取所需内容,复制它们,然后撤消.相反,也有flush-lines摆脱你不想要的线条.