【问题标题】:Emacs Case Sensitive Replace StringEmacs 区分大小写的替换字符串
【发布时间】:2011-03-17 22:35:17
【问题描述】:

我刚刚问了一个相关问题 (setq question),但它明显不同,所以我决定提出这个问题。

在我的.emacs 文件中,我定义了一个与replace-string 命令的键绑定:

(define-key global-map "\C-r" 'replace-string)

replace-string 进行基本的搜索和替换。假设搜索字符串的第一个字母是小写的,如果case-fold-searchnil,那么replace-string会区分大小写,否则不区分大小写。

问题在于case-fold-search 控制“搜索”(如search-forward 命令)和“搜索和替换”(如replace-string 命令)的“区分大小写”。

问题是我如何JUST使replace-string 命令(或任何C-r 绑定的命令)区分大小写,而使search-forward 默认不区分大小写.

也许我需要将case-fold-search 设置为nil 只是为了replace-string 命令,但我不知道该怎么做。

【问题讨论】:

    标签: emacs replace case-sensitive dot-emacs


    【解决方案1】:

    把它放在你的 .emacs 中:

    (defadvice replace-string (around turn-off-case-fold-search)
      (let ((case-fold-search nil))
        ad-do-it))
    
    (ad-activate 'replace-string)
    

    这正是您所说的,将case-fold-search 设置为nil 仅用于replace-string

    事实上这几乎就是Emacs Lisp reference manual中的例子。

    在 2021 年 11 月 2 日编辑: 如上面的链接所示,defadvice 不再是实现此功能的推荐方式。新的推荐实现是

    (defun with-case-fold-search (orig-fun &rest args)
      (let ((case-fold-search t))
        (apply orig-fun args)))
    
    (advice-add 'replace-string :around #'with-case-fold-search)
    

    【讨论】:

      【解决方案2】:

      试试这个不需要建议的方法:

      (global-set-key (kbd "C-r") 
          (lambda () 
            (interactive) 
            (let ((case-fold-search nil)) 
              (call-interactively 'replace-string))))
      

      【讨论】:

        猜你喜欢
        • 2011-03-01
        • 1970-01-01
        • 2010-09-21
        • 1970-01-01
        • 1970-01-01
        • 2014-04-11
        • 2012-02-08
        • 2013-03-14
        • 1970-01-01
        相关资源
        最近更新 更多