Emacs копируют согласующие отрезки длинной линии

В Emacs, как я могу легко скопировать все строки, соответствующие конкретному regex? Предпочтительно выделяя согласующие отрезки длинной линии, поскольку я ввожу.

occur добирается отчасти там путем привыкания их в буфер, но он добавляет много дополнительного материала.

15
задан Singletoned 18 February 2010 в 15:45
поделиться

3 ответа

Как насчет этого:

(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)))
12
ответ дан 1 December 2019 в 00:13
поделиться

Я счастливо использовал это долгое время:

    (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)
2
ответ дан 1 December 2019 в 00:13
поделиться

Вы можете использовать keep-lines , чтобы получить то, что вы хотите, скопировать их, а затем отменить. Напротив, есть также flush-lines , чтобы избавиться от ненужных строк.

9
ответ дан 1 December 2019 в 00:13
поделиться
Другие вопросы по тегам:

Похожие вопросы: