你如何告诉Emacs单独在搜索中替换字符串? [英] How do you tell Emacs to leave the case alone in a search and replace string?

查看:147
本文介绍了你如何告诉Emacs单独在搜索中替换字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Emacs中执行正则表达式搜索和替换(使用 Mx query-replace-regexp ),但通常有用的智能化案例正在阻碍。我的来源是:

  One 
Two
Three

我想用< item name =One/> 而不是。不幸的是,每一行开头的首都被误解,我正在使用大写字母< Item>



我可以找到关于如何使搜索区分大小写敏感的示例,以及如何在替换字符串中保留 \1 但没有关于如何保持整个替换字符串的情况 - 未修改。

解决方案

尝试将其添加到.emacs:

 (setq case-replace nil)

Ch v case-replace RET


文档:无 - query-replace'应该保留在
替换中的大小写。


还有一个指向替换命令和案例详细介绍与案例和适当变量的所有交互。



或者你可以定义一个新的命令,如:

 (defun query-replace-no-case )
(interactive)
(let((case-replace nil))
(call-interactiveively'query-replace)))
/ pre>

而且,如果您在一个函数中进行编码,并且只想暂时设置变量,那么您可以执行以下操作:

 (let((case-replace nil))
(while(search-forward ...)
(replace-匹配...)))


I'm trying to perform a regex search and replace in Emacs (using M-x query-replace-regexp), but the usually helpful smart case is getting in the way. My source is:

One
Two
Three

And I want to replace each line with something like <item name="One"/> instead. Unfortunately the capital at the start of each line is being misinterpreted, and I'm getting <Item> with an uppercase that I don't want.

I can find examples about how to make the search case sensitive, and how to keep the \1 lowercase in the replacement string, but nothing about how to keep the entire replacement string case-unmodified.

解决方案

Try adding this to your .emacs:

(setq case-replace nil)

C-h v case-replace RET:

Documentation: Non-nil means `query-replace' should preserve case in replacements.

And a link to the manual for Replace Commands and Case details all the interactions with case and the appropriate variables.

Or you could define a new command like:

(defun query-replace-no-case ()
   (interactive)
   (let ((case-replace nil))
       (call-interactively 'query-replace))))

And, if you were coding this up in a function, and only wanted to set the variable temporarily, you'd do something like:

(let ((case-replace nil))
   (while (search-forward ...)
       (replace-match ...)))

这篇关于你如何告诉Emacs单独在搜索中替换字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆