4

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.

Community
  • 1
  • 1
Roberto Aloi
  • 30,570
  • 21
  • 75
  • 112
  • Not a complete solution, but `occur` expects a regular expression so you can use `occur` to identify lines longer than 80 chars and they'll be displayed in a nice buffer where you can click on them and Emacs will automatically jump over to the corresponding line. – phimuemue Oct 28 '10 at 15:39

2 Answers2

4

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

Drew
  • 4,683
  • 4
  • 35
  • 50
3

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.

animuson
  • 53,861
  • 28
  • 137
  • 147
Trey Jackson
  • 73,529
  • 11
  • 197
  • 229