How to count the whole project code lines?
-
Suppose I open more than one files in Notepad++; is there a way or a plugin to count how many lines are in all my opened files?
-
Hello Paolo,
Certainly, someone that had some skills with the excellent Python plugin, of Dave Brotherstone, could write a very simple script, which sums the number of lines of each file opened, in the both views of a N++ session and returns the total !
As for me, I’m thinking about a simple search/replacement, in regular expression mode. Hi, Scott Sumner, don’t laugh at me ! Well, I sure that you’ll even find a more obvious way to do it :)
So, Paolo, just follow the steps below :
-
To begin, save all your opened files, of your current N++ session ( IMPORTANT )
-
Open the Replace dialog ( CTRL + H )
-
In the Find what zone, type in
\R|.\z
-
In the Replace with zone, type in
$0
-
Select the Regular expression radio button
-
Click on the button Replace All in All Opened Documents
=> You’ll get, at the bottom of the Replace dialog, the sentence :
Replace in Opened Files: nnn occurrences replaced.
- Close N++, discarding all the changes, on all opened files
NOTES :
-
Basically, this S/R change any End of Line character(s), of each line of a file, by… itself ! And, in case the last line doesn’t contain any EOF, it changes the very last standard character by itself, too ! Therefore, in all cases, it exactly performs one S/R per line :-)
-
Moreover, it works, whatever kind of End of Line characters, even mixed in a single document
-
The search regex
\R|.\z
tries to match any EOL ( \r\n, \n or \r ) OR the last standard character, at the very end of a file ( thanks to the syntax\z
) -
The replacement regex
$0
always stands for the entire matched search expression, which is just re-written !
Of course, a python script would be better, as it wouldn’t modify the opened files of a N++ session :-)
Best regards,
guy038
-
-
Normally I wouldn’t do this, but since guy038 mentioned me in his regex-related post, AND mentioned my #1 favorite thing about Notepad++ (its scriptability via the Pythonscript plugin), here’s a very quick short script that will do what is desired:
from Npp import * all_files_line_count = 0 tuple_list = notepad.getFiles() for tuple in tuple_list: filename = tuple[0] if filename == "new 0": continue notepad.activateFile(filename) this_files_line_count = editor.getLineCount() all_files_line_count += this_files_line_count notepad.messageBox("The total line count of all open files is: " + str(all_files_line_count), "", MESSAGEBOXFLAGS.OK)
This is all really basic stuff if one takes a look at the Pythonscript plugin documentation, and one is a bit familiar with Python 2.x. The only slightly tricky thing is the check against the “new 0” filename–this is sort of a phantom file that isn’t really one of the open ones to be counted, but regardless when I was testing this on my system it had 677 lines in it. The Python code uses the “continue” statement if the “new 0” file is encountered in the loop over all of the filenames; this causes the rest of the indented loop to be skipped, and processing started with the next file in the list. The bottom line is that the lines in “new 0” are not summed with the rest of the files.
-
Hi Paolo and Scott,
Fine, your python script, Scott ! Indeed, with my S/R, it would have counted, wrongly, any stuff, in the New 0 tab !
So, as usual, the nice thing aboutN++, is that there are, always, several ways to solve the same problem :))
Cheers,
guy038
-
guy038,
No, I think your regex count method would work just fine; in other words, it wouldn’t see the “new 0” file. The “new 0” file I’m talking about doesn’t seem to be a real thing to the user, just something that gets returned with the notepad.getFiles() function of Pythonscript. Then again, it could be a file that means something special to N++ behind the scenes (and thus at the Pythonscript level it IS a real file), but I don’t know. Maybe Dave B is watching, knows the answers, and will comment.
Related: There are times in N++ when I see a “new _” file created briefly when I didn’t do it. [Side note: I rarely create a new file in the tradition manner; I almost always create a new file with the TakeNotes plugin, so I don’t have files of my own creation called “new _” in my tabs.] One example of this is when N++ is shutting down, I believe, sometimes I see a “new 1” tab created very briefly as N++ is cleaning up. I’d like to know more about these rogue “new _” files, if anyone knows…