Quick way to get SLOC

Dhruva Sagar
TarkaLabs TIL
Published in
1 min readJan 24, 2023

Source lines of code (SLOC), also known as lines of code (LOC), is a software metric used to measure the size of a computer program by counting the number of lines in the text of the program’s source code. SLOC is typically used to predict the amount of effort that will be required to develop a program, as well as to estimate programming productivity or maintainability once the software is produced.

Add this function to your favorite shell config file ~/.zshrc or ~/.bashrc

sloc () {
git ls-files | grep "$1" | grep -v grep | xargs wc -l
}

This depends on git , grep, wc and xargs which are pre-installed in most linux distributions as well as mac osx.

This is what this looks like :

h4x0rdud3@h4x0rdud3 in ~/dotfiles/vim/pack/packup/start/vim-zoom on master ✔
λ sloc
79 README.md
72 autoload/zoom.vim
94 doc/zoom.txt
16 plugin/zoom.vim
261 total

If you wanted to get SLOC for a specific file type, just add the file extension as an argument to sloc, for example sloc vim gives you :

h4x0rdud3@h4x0rdud3 in ~/dotfiles/vim/pack/packup/start/vim-zoom on master ✔
λ sloc vim
72 autoload/zoom.vim
16 plugin/zoom.vim
88 total

--

--

Dhruva Sagar
TarkaLabs TIL

hacker. loves to code, lives inside terminals, vim advocate, open source enthusiast.