Is there a way to display the sloc? I have searched every where and all I get is how to display total lines or how to show line numbers.

  • 3
    What do you want to do with it? For example, if I have cloc installed I might just run :!cloc. Or i could do my own command :w !grep -v \# | wc -l, etc. Afaik no plugin exists simply because “sloc” is not always easy to define.
    – D. Ben Knoble
    Sep 8, 2020 at 22:37
  • I was thinking about adding it to my status line, it would be easier with a plugin, but running :!cloc is enough too
    – user419050
    Sep 9, 2020 at 18:55
  • You can get the output of cloc using system/systemlist, which you can manipulate and put in the statusline. But be warned: running an external command directly in the statusline can slow down vim (it may get called on each statusline update!)
    – D. Ben Knoble
    Sep 9, 2020 at 18:56
  • @D.BenKnoble I would attempt this, but I don't really know any vim script
    – user419050
    Sep 10, 2020 at 15:46
  • Perhaps you can have a mapping in the likes of noremap <leader><space> :!cloc %<cr> . And then you can just check the sloc by pressing your leader key and space. Feb 28, 2021 at 9:35

1 Answer 1


Note that there is an almost duplicate question: How to read/get the number of lines of code in the buffer or a directory?

Now, if you want that number in the statusline... As @D. Ben Knoble said, this will be really slow.

I would be tempted to to do it in vimscript, but even there, filtering all the comments out the buffer, is not exactly something fast as the only way that really works in vimscript with multilines comments is by analyzing the syntax highlighting, character by character.

In my lh-vim-lib library plugin, I've bricks to filter a buffer based on the syntax highlighting:

:let no_comment = lh#syntax#line_filter('\ccomment')
" I use Vim 8.{recent} features in the next line.
:echo range(1, line('$'))->map({_,v -> g:no_comment.getline_not_matching(v)})->filter({_, v -> v !~ '^\s*$' })->len()

But still I doubt this will be fast enough to integrate in the statusline.

If this isn't already handled through LSP servers, I'd say the fasted approach would probably to run cloc in the background on TextChanged* events (as long it isn't already running) and use the result to update a cached variable used in the statusline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.