31

replace-string是否可以在 emacs 中的矩形区域内执行操作?如果是这样,怎么做?

4

4 回答 4

29

如果启用 CUA 选择模式:

M-x cua-selection-mode RET

或永久保存在您的初始化文件中:

(cua-selection-mode 1)

然后,您可以使用其高级矩形编辑工具。

(或者如果你是cua-mode用户,那么你不需要这样做。)

  • C-RET标记一个角落。
  • 将点移动到对角。
  • M-r在标记的矩形内进行正则表达式替换。
  • C-RET取消标记/退出矩形编辑。

有关文档,请在注释中查找“CUA 矩形支持”标题M-x find-library RET cua-base RET

如果您出于某种原因不想使用 cua 矩形工具(也许如果您真的replace-string特别需要),那么使用自定义函数apply-on-rectangle将非常简单地放在一起。

编辑:实际上,比我预期的要复杂一些,但大部分代码是交互式规范和对“分隔”前缀参数行为的支持(都基于replace-string)。

编辑 2:我认为这值得以更全面的方式进行:

以下提供C-xrM-%C-xrC-M-%,它(希望)按照您期望的方式运行。

(require 'rect)

(defun my-search-replace-in-rectangle
  (start end search-pattern replacement search-function literal)
  "Replace all instances of SEARCH-PATTERN (as found by SEARCH-FUNCTION)
with REPLACEMENT, in each line of the rectangle established by the START
and END buffer positions.

SEARCH-FUNCTION should take the same BOUND and NOERROR arguments as
`search-forward' and `re-search-forward'.

The LITERAL argument is passed to `replace-match' during replacement.

If `case-replace' is nil, do not alter case of replacement text."
  (apply-on-rectangle
   (lambda (start-col end-col search-function search-pattern replacement)
     (move-to-column start-col)
     (let ((bound (min (+ (point) (- end-col start-col))
                       (line-end-position)))
           (fixedcase (not case-replace)))
       (while (funcall search-function search-pattern bound t)
         (replace-match replacement fixedcase literal))))
   start end search-function search-pattern replacement))

(defun my-replace-regexp-rectangle-read-args (regexp-flag)
  "Interactively read arguments for `my-replace-regexp-rectangle'
or `my-replace-string-rectangle' (depending upon REGEXP-FLAG)."
  (let ((args (query-replace-read-args
               (concat "Replace"
                       (if current-prefix-arg " word" "")
                       (if regexp-flag " regexp" " string"))
               regexp-flag)))
    (list (region-beginning) (region-end)
          (nth 0 args) (nth 1 args) (nth 2 args))))

(defun my-replace-regexp-rectangle
  (start end regexp to-string &optional delimited)
  "Perform a regexp search and replace on each line of a rectangle
established by START and END (interactively, the marked region),
similar to `replace-regexp'.

Optional arg DELIMITED (prefix arg if interactive), if non-nil, means
replace only matches surrounded by word boundaries.

If `case-replace' is nil, do not alter case of replacement text."
  (interactive (my-replace-regexp-rectangle-read-args t))
  (when delimited
    (setq regexp (concat "\\b" regexp "\\b")))
  (my-search-replace-in-rectangle
   start end regexp to-string 're-search-forward nil))

(defun my-replace-string-rectangle
  (start end from-string to-string &optional delimited)
  "Perform a string search and replace on each line of a rectangle
established by START and END (interactively, the marked region),
similar to `replace-string'.

Optional arg DELIMITED (prefix arg if interactive), if non-nil, means
replace only matches surrounded by word boundaries.

If `case-replace' is nil, do not alter case of replacement text."
  (interactive (my-replace-regexp-rectangle-read-args nil))
  (let ((search-function 'search-forward))
    (when delimited
      (setq search-function 're-search-forward
            from-string (concat "\\b" (regexp-quote from-string) "\\b")))
    (my-search-replace-in-rectangle
     start end from-string to-string search-function t)))

(global-set-key (kbd "C-x r M-%") 'my-replace-string-rectangle)
(global-set-key (kbd "C-x r C-M-%") 'my-replace-regexp-rectangle)
于 2012-06-21T02:18:38.037 回答
5

M-%在最近的 Emacs(例如 Emacs 25)中,您可以使用( query-replace) 或C-M-%( )开箱即用地执行此操作query-replace-regexp

用于M-x rectangle-mark-mode创建矩形区域。C-x C-x必要时使用,将点放在标记之前。然后只需查询替换。

然而,他们并没有在 Emacs 25 for replace-string. 如果你愿意,你可以做到这一点,只需像region-noncontiguous-pquery-replace. 代码很简单:

(defun replace-string (from-string to-string &optional delimited start end backward
                       region-noncontiguous-p)
  "..."
  (declare (interactive-only
            "use `search-forward' and `replace-match' instead."))
  (interactive
   (let ((common
          (query-replace-read-args
           (concat "Replace"
                   (if current-prefix-arg
                       (if (eq current-prefix-arg '-) " backward" " word")
                     "")
                   " string"
                   (if (use-region-p) " in region" ""))
           nil)))
     (list (nth 0 common) (nth 1 common) (nth 2 common)
           (if (use-region-p) (region-beginning))
           (if (use-region-p) (region-end))
           (nth 3 common)
           (if (use-region-p) (region-noncontiguous-p)))))
  (perform-replace
    from-string to-string nil nil delimited nil nil start end backward region-noncontiguous-p))

或者,您可以只下载库replace+.el并使用replace-string那里的版本。它做你想做的事。

FWIW,我刚刚提交了 Emacs bug#27897以将此功能添加到replace-string同一库中的其他几个命令中,replace.el.

于 2017-07-23T21:59:12.073 回答
1

对于邪恶的用户,你可以使用我的包evil-visual-replace来获取query-replacereplace-regexp进入邪恶的视觉块。

于 2016-08-11T21:18:40.247 回答
0

如果您处于 CUA 模式,则可以使用绑定到“先生”的 cua-replace-in-rectangle。

于 2013-11-07T23:23:56.447 回答