Emacs copy matching lines

后端 未结 5 1900
北恋
北恋 2020-12-13 16:13

In Emacs how can I easily copy all lines matching a particular regex? Preferably highlighting the matching lines as I type.

occur gets partway there by

5条回答
  •  渐次进展
    2020-12-13 16:40

    I've been using this happily for a long time:

        (defun occur-mode-clean-buffer ()
      "Removes all commentary from the *Occur* buffer, leaving the
    unadorned lines."
      (interactive)
      (if (get-buffer "*Occur*")
          (save-excursion
            (set-buffer (get-buffer "*Occur*"))
            (fundamental-mode)
            (goto-char (point-min))
            (toggle-read-only 0)
            (set-text-properties (point-min) (point-max) nil)
            (if (looking-at (rx bol (one-or-more digit)
                                (or " lines matching \""
                                    " matches for \"")))
                (kill-line 1))
            (while (re-search-forward (rx bol
                                          (zero-or-more blank)
                                          (one-or-more digit)
                                          ":")
                                      (point-max)
                                      t)
              (replace-match "")
              (forward-line 1)))
    
        (message "There is no buffer named \"*Occur*\".")))
    
    (define-key occur-mode-map (kbd "C-c C-x") 'occur-mode-clean-buffer)
    

提交回复
热议问题