可以在emacs的矩形区域内执行replace-string
的操作吗?如果是这样,怎么样?在emacs的矩形区域内搜索和替换
回答
如果启用CUA选择模式:
M-Xcua-selection-mode
RET在init文件
或永久:
(cua-selection-mode 1)
然后可以使用其先进的矩形的编辑功能。
(或者如果你是一个cua-mode
用户,那么你就不需要做。)
- C-RET标记一个角落。
- 将点移动到对角。
- M-r做一个10正则表达式替换在标记的矩形内。
- C-RET取消标记/退出矩形编辑。
有关文档,查找 “CUA矩形支持” MX在评论中标题find-library
RETcua-base
RET
如果你不想使用的CUA矩形设施某种原因(也许如果你确实需要replace-string
),使用apply-on-rectangle
的自定义函数将非常简单地放在一起。
编辑:其实,稍微复杂一点比我预想的,但大部分代码是交互规范,对于“分隔” prefix参数的行为支持(包括基于replace-string
)。
编辑2:我决定,这是值得做的一个功能更全面的方式:
下面提供CX[RM-%和CX[RCM - %,(希望)按照您的预期行事。
(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)
如果您处于CUA模式,可以使用绑定到'M-r'的cua-replace-in-rectangle。
但是这只会替换事件的第一次出现(默认vim replace-regexp行为)。 – Adobe
对于恶意用户,您可以使用我的包evil-visual-replace获得query-replace
和replace-regexp
内部邪恶的视觉块。
在最近的Emacs(例如Emacs的25),你可以做到这一点,利用M-%
(query-replace
)或C-M-%
(query-replace-regexp
)的开箱。使用M-x rectangle-mark-mode
创建矩形区域。必要时使用C-x C-x
,在标记之前放置点。然后只是查询替换。
但是,他们没有在Emacs 25中对replace-string
进行相同的更改。你可以这样做,如果你喜欢,只需要以query-replace
的方式添加参数region-noncontiguous-p
即可。代码很简单:
(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
。
还有一个anzu bug(可能会替代你的'query-replace'版本):https://github.com/syohex/emacs-anzu/issues/69 –
- 1. Emacs(TeX):如何搜索和替换整个区域?
- 2. emacs的搜索throught矩形
- 3. 给区域搜索emacs
- 4. 谷歌的radarSearch搜索矩形而不是圆形区域?
- 5. 如何用yank缓冲区内容替换emacs中的区域?
- 6. 在Emacs中搜索并替换冰柱
- 7. emacs新手问题 - 如何在一个区域内搜索
- 8. 矩形区域和边界
- 9. 使用矩形在图像上创建搜索区域
- 10. 在OSX Lion下查询替换emacs内部区域Lion
- 11. 谷歌或bing地图中的矩形区域搜索
- 12. 搜索和替换的字符在Emacs序列
- 13. 如何在emacs中进行大量的搜索和替换?
- 14. 搜索和替换
- 15. 搜索和替换在Vi
- 16. 如何提取视频的矩形和非矩形区域?
- 17. 的Javascript搜索和替换
- 18. 搜索并替换相同的可视区域
- 19. Emacs恶意模式搜索和替换 - 错误的参数数
- 20. Emacs搜索和替换的好教程+参考?
- 21. Android矩形区域检测
- 22. Custon UISlider非矩形区域
- 23. sierpinski地毯 - 矩形区域
- 24. emacs lisp - 文本搜索和替换列表到模板
- 25. 更新图形中的矩形区域
- 26. Python的搜索和替换在StringIO缓冲区?
- 27. 搜索和文本区域的Java替代
- 28. VIM搜索和替换
- 29. CouchDB搜索和替换
- 30. JavaScript搜索和替换?
谢谢!不幸的是,我似乎只能在emacs GUI中用'C-RET'标记一个角落,而不是终端模式。 – user545424
这将是由于您的终端不发送该序列。您可能会发现xterm更成功(另请参阅http://stackoverflow.com/questions/4337837/send-c-to-emacs-in-vt100-xterm-terminal-mac-os-xs-terminal)。我还用非CUA方法的更全面的代码更新了我的答案。 – phils
功能请求:搜索矩形。我有一个巨大的专栏,我需要检查它是否有别的,但'DPP'。我使用了一些复杂的正则表达式 - 如果列很大 - 我得到正则表达式堆栈溢出。 – Adobe