如何在整个缓冲区中搜索和替换?


17

M-%和搜索和替换!是从当前位置到缓冲区末尾。如何在整个缓冲区中执行此操作?谢谢。


2
我建议将标题更改为“搜索并替换整个缓冲区”。在全球范围内也可以指整个项目。
马拉巴巴2014年

1
这是Vim / Evil难以击败的领域::%s/foo/bar
shosti

@shosti:实际上,我认为您的方法需要更多的击键。只是说';-)
nispio

Answers:


14

我看不到该支持,但仍然保留您的起始位置。(当搜索到达末尾时,我看不到将其包装到缓冲区的开头的方法。)

最好的选择是M-<先进入缓冲区的开始,然后执行query-replace,完成后按C-uC-spaceC-uC-space跳回到起点。


1
这工作时transient-mark-mode处于开启状态。否则C-SPC C-SPC将暂时启用transient-mark-mode
nispio 2014年

5
无需使用C-SPC手动设置标记。M-<(以及许多其他可能“长途移动”的命令)可以为您完成此任务。
Mathias Dahl 2014年

9

您可以将以下命令添加到emacs初始化文件,并将其绑定到您选择的按键。

(defun replace-regexp-entire-buffer (pattern replacement)
  "Perform regular-expression replacement throughout buffer."
  (interactive
   (let ((args (query-replace-read-args "Replace" t)))
     (setcdr (cdr args) nil)    ; remove third value returned from query---args
     args))
  (save-excursion
    (goto-char (point-min))
    (while (re-search-forward pattern nil t)
      (replace-match replacement))))

9

您可以按照以下步骤操作:

  • C-x h—选择整个缓冲区 M-< -转到缓冲区顶部
  • M-% —发起 query-replace
  • ! —强制全部替换
  • C-u C-SPC C-u C-SPC —返回您的起始位置

这应该引起更多关注。
因德拉

3

您可以将其添加到init.el文件中以更新行为,M-%以默认情况下替换整个缓冲区中的单词:

(defun my/query-replace (from-string to-string &optional delimited start end)
  "Replace some occurrences of FROM-STRING with TO-STRING.  As each match is
found, the user must type a character saying what to do with it. This is a
modified version of the standard `query-replace' function in `replace.el',
This modified version defaults to operating on the entire buffer instead of
working only from POINT to the end of the buffer. For more information, see
the documentation of `query-replace'"
  (interactive
   (let ((common
      (query-replace-read-args
       (concat "Query replace"
           (if current-prefix-arg " word" "")
           (if (and transient-mark-mode mark-active) " in region" ""))
       nil)))
     (list (nth 0 common) (nth 1 common) (nth 2 common)
       (if (and transient-mark-mode mark-active)
           (region-beginning)
         (buffer-end -1))
       (if (and transient-mark-mode mark-active)
           (region-end)
         (buffer-end 1)))))
  (perform-replace from-string to-string t nil delimited nil nil start end))
;; Replace the default key mapping
(define-key esc-map "%" 'my/query-replace)

并从获得相同的行为query-replace-regexp

(defun my/query-replace-regexp (regexp to-string &optional delimited start end)
  "Replace some things after point matching REGEXP with TO-STRING.  As each
match is found, the user must type a character saying what to do with
it. This is a modified version of the standard `query-replace-regexp'
function in `replace.el', This modified version defaults to operating on the
entire buffer instead of working only from POINT to the end of the
buffer. For more information, see the documentation of `query-replace-regexp'"
  (interactive
   (let ((common
      (query-replace-read-args
       (concat "Query replace"
           (if current-prefix-arg " word" "")
           " regexp"
           (if (and transient-mark-mode mark-active) " in region" ""))
       t)))
     (list (nth 0 common) (nth 1 common) (nth 2 common)
       (if (and transient-mark-mode mark-active)
           (region-beginning)
         (buffer-end -1))
       (if (and transient-mark-mode mark-active)
           (region-end)
         (buffer-end 1)))))
  (perform-replace regexp to-string t t delimited nil nil start end))
;; Replace the default key mapping
(define-key esc-map [?\C-%] 'my/query-replace-regexp)

很有用。谢谢。
NVaughan

2

如果使用冰柱,则可以搜索并替换整个缓冲区(或多个缓冲区或文件或书签目标)。

query-replace(例如C-x h M-%)不同:

  • 您可以按任何顺序浏览匹配项。

  • 替换是按需的:您无需访问每个比赛并回答是否替换它。


0

这是我当前正在使用的解决方案,它从缓冲区的开头开始,并且在替换后会回到旧的点。

(defun query-replace-from-top ()
  (interactive)
  (let ((orig-point (point)))
    (save-excursion
      (goto-char (point-min))
      (call-interactively 'query-replace))
    (message "Back to old point.")
    (goto-char orig-point)))
(bind-key* "M-%" 'query-replace-from-top)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.