[英]emacs search and replace with regular expressions
With emacs, how can I search and replace regular expressions within a buffer? 使用emacs,如何在缓冲区中搜索和替换正则表达式? How can I do this programmatically by evaluating elisp in addition to interactively?
除了交互评估外,如何通过编程评估elisp来做到这一点? For example, replace one or more spaces with tabs, where I need to match
' +'
(one or more spaces) with something like Cq-TAB
. 例如,用制表符替换一个或多个空格,在这里我需要将
' +'
(一个或多个空格)与Cq-TAB
匹配。 Is this possible? 这可能吗?
我相信答案是Mx replace-regexp
Programmatically you can do it as follows: 以编程方式,您可以这样做:
(defun region-replace-multiple-spaces-with-single-space(beg end)
(interactive "*r")
(save-restriction
(narrow-to-region beg end)
(save-excursion
(goto-char (point-min))
(while (search-forward-regexp " +" nil t)
(replace-match " " nil nil)))))
I've got a multi-purpose replace-regexp
: 我有一个多功能的
replace-regexp
:
query-replace-regexp
query-replace-regexp
Here's the code: 这是代码:
(global-set-key (kbd "C-/") 'replace-regexp-dwim)
(defun replace-regexp-dwim (arg)
(interactive "P")
(destructuring-bind (from to &rest)
(query-replace-read-args "Replace regexp" nil)
(if arg
(query-replace-regexp from to)
(let ((st (if (region-active-p)
(region-beginning)
(line-beginning-position)))
(en (if (region-active-p)
(region-end)
(line-end-position))))
(goto-char st)
(while (re-search-forward from en t)
(replace-match to nil t))))))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.