Emacs 区分大小写替换字符串
我刚刚问了一个相关的问题(setq 问题),但它明显不同,所以我决定分支这个问题。
在我的 .emacs
文件中,我定义了一个与 replace-string
命令的键绑定:
(define-key global-map "\C-r" 'replace-string)
replace-string
执行基本的搜索和替换。假设搜索字符串的第一个字母是小写,如果 case-fold-search
为 nil
则 replace-string
进行区分大小写的搜索,否则进行不区分大小写的搜索。
问题在于 case-fold-search
控制“搜索”(如 search-forward
命令)和“搜索和替换”(就像 replace-string
命令)。
问题是如何使 JUST replace-string
命令(或 Cr
绑定的任何内容)区分大小写,而保留 >search-forward
默认情况下不区分大小写。
也许我需要将 case-fold-search
设置为 nil
只是为了 replace-string
命令,但我不知道如何这样做。
I just asked a related question (setq question) but it's distinctly different, so I decided to branch off with this question.
In my .emacs
file, I define a key binding to the replace-string
command:
(define-key global-map "\C-r" 'replace-string)
replace-string
does basic search and replace. Assuming the first letter of the search string is lowercase, if the case-fold-search
is nil
then replace-string
does case-sensitive search, otherwise it does case-insensitive search.
The problem is that case-fold-search
controls the "case-sensitiveness" of both "search" (like the search-forward
command) and "search and replace" (like the replace-string
command).
The question is how do I make JUST the replace-string
command (or anything C-r
is bound to) case-sensitive, leaving the search-forward
case-insensitive as it is by default.
Perhaps I would need to set case-fold-search
to nil
just for the replace-string
command, but I'm not sure how to do that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将其放入您的 .emacs 中:
这正是您所说的,将
case-fold-search
设置为nil
只是为了replace-string
。事实上,这几乎正是 中的示例Emacs Lisp 参考手册。
于 2021 年 11 月 2 日编辑: 如上面的链接所示,
defadvice
不再是实现此目的的推荐方法。新的建议实施是Put this in your .emacs:
This does exactly what you said, set
case-fold-search
tonil
just forreplace-string
.In fact this is almost exactly the example in the Emacs Lisp reference manual.
Edit on 2021-11-02: as the link above indicates,
defadvice
is no longer the recommended way to implement this. The new recommended implementation would be尝试这个方法,它不需要建议:
Try this method, which does not require advice: