Here's a way of getting the information:
com! -range=% -nargs=? -complete=file CLOC call s:cloc(<line1>,<line2>,<q-args>)
nno <silent> g<C-l> :CLOC<CR>
xno <silent> g<C-l> :CLOC<CR>
fu! s:cloc(lnum1,lnum2,path) abort
if !empty(a:path)
let to_scan = a:path
else
let file = tempname()
let to_scan = file.'.'.&ft
let lines = getline(a:lnum1, a:lnum2)
call writefile(lines, to_scan)
endif
let cloc_output = matchstr(system('cloc '.to_scan), 'http.\{-}\n\zs.*\ze\n')
if empty(a:path)
call delete(to_scan)
endif
let to_display = cloc_output
let cloc_output = map(filter(split(cloc_output, "\n"), 'v:val =~# ''\d\+'''),
\ 'split(v:val, ''\s\{2,}\ze\d'')')
let g:cloc_results = {}
let keys = ['files', 'blank', 'comment', 'code']
for values_on_line in cloc_output
let i = 0
let dict = {}
for value in values_on_line[1:]
let dict[keys[i]] = eval(value)
let i += 1
endfor
let g:cloc_results[values_on_line[0]] = dict
endfor
echo to_display
endfu
It relies on the cloc
shell command, which, on debian based distributions, can be installed with:
sudo apt-get install cloc
It can also be downloaded from github.
It installs normal and visual mode mappings, both using the key sequence g C-l
(l
for lines).
The normal mapping allows to read the number of lines of code, comments and blank lines in the current buffer. The visual mapping allows to get the same info for the visual selection.
It also installs the Ex command :CLOC
, which accepts a range (matching the whole buffer by default), and a path to a file or a folder.
When passed a path, :CLOC
should scan all the files under the latter.
Both the mappings and the Ex command should populate the global variable g:cloc_results
with a dictionary.
Each key of this dictionary should be a programming language (except one named SUM:
).
Each value should be another dictionary. This sub-dictionary should contain the 4 following keys:
files " number of files under the path whose language is the name of the dictionary
code " total lines of code for this language
comment " total comment lines "
blank " total blank lines "

Inside a script, where the only thing desired is the global variable (no message) :CLOC
could be executed silently:
silent CLOC /path
Then, one could access the data for a given language, let's say python, like this:
let yourvar = g:cloc_results.Python
It should give something like:
{'comment': 2213, 'files': 93, 'code': 10970, 'blank': 2630}
And if the language contains a space, like Bourne Shell
, you could use this other syntax, where the space is protected in a string:
let yourvar = g:cloc_results['Bourne Shell']