开发者

Recursively identifying lines which are longer than 80 characters in Emacs

开发者 https://www.devze.com 2023-01-22 15:32 出处:网络
Since it may be a good idea to have lines that are not wider than 80 characters in code files, what\'s the most effective way of recursively identify these lines in an existing project using Emacs?

Since it may be a good idea to have lines that are not wider than 80 characters in code files, what's the most effective way of recursively identify these lines in an existing project using Emacs?

UPDATE:

Using Trey's suggestion, I'm currently using the following code:

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

Which works fine in combination with whitespace-mode.


I can't top EMACS specific solutions, but you can alter the command a bit to include file names (and type less).

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


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

Quotes not necessary for interactive rgrep prompt.

rgrep RET ^.{81} RET file-type RET path


You could use igrep-find and use a regular expression that matches 81(+) characters like so:

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

And then you get a buffer in compilation mode that lets you easily jump to each of the offending lines (either via a mouse click, or C-x ` or M-x next-error).

Alternatively, you could use the built-in M-x grep-find and use the same regular expression.

To enter 81 ., type C-u 81 ..

And, if you want to have this all contained in a single command (that prompts you for the path to the files), you can use this:

(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))

There are some other tips available on the Emacs Wiki for Find Long Lines, including several options for highlighting lines that are longer than 80 characters when you're visiting a file.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号