I'm trying to get the count of lines in the current file using vimscript but I can't figure out how (and google is returning a bunch of crap about showing line numbers).
The simplest way is to press ctrl-g
, which will reveal the filename, current line, the line count, your current position as a percentage, and your cursor's current column number.
when you select an area, then vim shows in corner how many lines you have selected
if you have following in your .vimrc file:
set statusline=%f\ %l,%c
-
4If the OP wants to use that number back into a vim script, neither
'statusline'
norCTRL-G
will be helpful to him.line('$')
will return the number of lines in the current buffer, andlen(readfile(filename))
the number of lines in a given file. – Luc Hermitte Feb 8 '17 at 13:23 -
-
Indeed, the title says nothing about this. However, in the text, the OP has specified "using vimscript". He may have expressed a mean(s?) that restricts, for no good reason, valid solutions to his real objective. In that case your answer and matty's answer would be perfectly valid. I cannot tell. – Luc Hermitte Feb 8 '17 at 14:10
-
2
The variable %L
already contains the total number of lines.
You could use :echo %L
or :set statusline+=%L
to append it to the status
-
your example ':echo %L' doesn't work in gvim 7.4.580 on linux nor in 8.0.6 on windows. The statusline example worked, but please include the version of vim one can use '%L' as a target of 'echo' on the vim-cmdline(':') for version/platform specific features. Thanks! – Astara Oct 20 '20 at 0:50
:help functions
. – romainl Nov 14 '12 at 7:47