phi*_*ils 26
如果启用CUA选择模式:
M-x cua-selection-mode RET
或永久在您的init文件中:
(cua-selection-mode 1)
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用其高级矩形编辑工具.
(或者,如果您是cua-mode用户,那么您不需要这样做.)
有关文档,请在评论中查找"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)
Run Code Online (Sandbox Code Playgroud)
在最近的Emacs(例如Emacs 25)中,您可以使用M-%(query-replace)或C-M-%(query-replace-regexp)开箱即用.
使用M-x rectangle-mark-mode以创建一个矩形区域.C-x C-x必要时使用,在标记前加点.然后只是查询替换.
然而,他们没有在Emacs 25中做出同样的改变replace-string.如果你愿意,你可以这样做,只需添加参数region-noncontiguous-p就像完成它一样query-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))
Run Code Online (Sandbox Code Playgroud)
或者,您可以下载库replace+.el并使用其中的版本replace-string.它做你想要的.
FWIW,我刚刚提交了Emacs 错误#27897,以便replace-string在同一个库中添加此功能以及其他几个命令replace.el.