【问题标题】:Recursively identifying lines which are longer than 80 characters in Emacs递归识别 Emacs 中超过 80 个字符的行
【发布时间】:2011-05-01 23:08:17
【问题描述】:

由于it may be a good idea to have lines that are not wider than 80 characters in code files,在使用 Emacs 的现有项目中递归识别这些行的最有效方法是什么?

更新:

根据 Trey 的建议,我目前正在使用以下代码:

(defun find-long-lines (base-dir)
  "Recursively look for lines longer than 80 characters files"
  (interactive "DPath:")
  (grep-compute-defaults)
  (rgrep "^................................................................................." "*" base-dir))

whitespace-mode 结合使用效果很好。

【问题讨论】:

  • 不是一个完整的解决方案,但 occur 需要一个正则表达式,因此您可以使用 occur 来识别超过 80 个字符的行,它们将显示在一个漂亮的缓冲区中,您可以在其中单击它们和 Emacs 会自动跳转到相应的行。

标签: emacs coding-style


【解决方案1】:

我无法超越 EMACS 特定的解决方案,但您可以稍微更改命令以包含文件名(并键入 less)。

rgrep "^.\{81\}" . -n (include line numbers)


rgrep "^.\{81\}" . -c (summary view per file)

交互式 rgrep 提示不需要引号。

rgrep RET ^.{81} RET 文件类型 RET 路径

【讨论】:

    【解决方案2】:

    您可以使用 igrep-find 并使用匹配 81(+) 个字符的正则表达式,如下所示:

    M-x igrep-find ^.................................................................................. RET /path/to/area/to/search/* RET
    

    然后你会在编译模式下获得一个缓冲区,让你可以轻松地跳转到每个有问题的行(通过鼠标单击,或 Cx `Mx next-error kbd>)。

    或者,您可以使用内置的M-x grep-find 并使用相同的正则表达式。

    要输入 81 .,请输入 C-u 81 .

    而且,如果您想将所有这些都包含在一个命令中(提示您输入文件的路径),您可以使用以下命令:

    (defun find-lines-longer-than-80 (files)
      "Recursively look for lines longer than 80 characters files"
      (interactive (list (igrep-read-files)))
      (igrep igrep-program "^................................................................................." files igrep-options))
    

    在 Emacs Wiki 上为 Find Long Lines 提供了一些其他提示,包括在您访问文件时突出显示长度超过 80 个字符的行的几个选项。

    【讨论】:

    • 要输入81个点,您可以输入ESC 8 1 .C-u 8 1 .
    • @Giles 我应该提到这一点,这正是我所做的。
    • 谢谢。对当前缓冲区使用空白模式,它就像一个魅力。您的解决方案不会递归遍历所有子文件夹,是吗?
    • 我已经更新了我的问题。该代码似乎工作正常。非常感谢您的建议。
    • @RobertoAloi 该解决方案确实会遍历所有子文件夹(即igrep-findgrep-find-find 部分。您可以使用igrep 或将其限制为单个文件夹grep 命令。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    相关资源
    最近更新 更多