In Vim, how do you get the number of lines in the current file using vimscript?

vim show line numbers
vim show percentage of file
vim statusline
count string occurrences in vi editor
vim show relative and absolute line numbers
how to enable line in vim
how do i permanently set line numbers in vim
count number of lines in a file linux

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).

You can use line() function:

:echo line('$')

Status Lines / Learn Vimscript the Hard Way, If you are in linux you can use wc -m to get the character count in the current file :! wc -m % (which then reports back the number of matched chars) or that you do a fancy strlen() on the visually selected text: For the those less familiar with vimscript foo, I did :command! :set statusline+=\ %{\ line2byte(line(\"$\")+1)-1\ }B . How to enable line number setting permanently while using vim. If you need number every time you start vi/vim, append the following line to your ~/.vimrc file: $ vi ~/.vimrc Append the following line: set number Save and close the file in vim. How to open a file at particular location/line number. Jump to particular line number from a shell

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.

How can I get (g)Vim to display the character count of the current file , The one-page guide to Vimscript functions: usage, examples, links, snippets, and more. current line as a string getline(1) " get line 1 getline(1, 5) " get lines 1-5 search('^$') " next blank line, returns line number search('^$' expand('<cword>') " word under cursor expand('%') " current file " <cword> current word on cursor� Vim doesn’t show line numbers by default, but you can easily enable it in your configuration. When you are working on a bash script and debugging an issue, then you can debug your issue easily by pin-pointing the line number. Vim has three modes to help navigate around files:

You could also use

wc -l <filename>

Vimscript functions cheatsheet, This tip, however, shows how to insert line numbers into a file, or into just a section. Also, the option to print with line numbers is given (Vim can print lines with That expression uses printf() to format the number of the current line: %-4d is a Use the following if you want to only number non-blank lines (it finds the start of a� If you want line numbers to appear each time you launch Vim, add the appropriate command to your .vimrc (Vim configuration file). For example, to enable absolute line numbering, you would add the following: vim ~/.vimrc

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

Insert line numbers | Vim Tips Wiki, For example, in directory /abc the command vim def/my.txt would edit file :echo expand('%:p:h:t'), def, First get the full path with :p ( /abc/def/my.txt ) The following commands insert lines consisting of the full path of the current If you need to insert other file names (say the *.h name in an #include), you can do C-X +C-F� Will read last 10 lines from xyz.xml into current buffer where the cursor is. The head and tail commands are extremely fast, therefore even combining them can be much faster than other approaches for very large files.:r !head -700030 xyz.xml| tail -30 Will read line numbers from 700000 to 700030 from file xyz.xml into current buffer

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

Get the name of the current file | Vim Tips Wiki, Option arguments and file name arguments can be mixed, and any number of them The first one will be the current file and read into the buffer. -+ +[num] The cursor will be positioned on line "num" for the first file being edited. You can use this to get 'nocompatible', when there is no .vimrc file or when using "-u NONE". set the status line. more info with :help statusline. These commands can go in your .vimrc file, or you can enter them as commands while in vim by typing ':' in command mode. First, set last status to 2 using the following: set laststatus=2. Then set status line to %f for short file name. set statusline=%f. For the full path to the file, use %F.

Vim: starting.txt, While running Vim a lot of help can be obtained from the on-line help system, with the ":help" command. The first one will be the current file and read into the buffer. You can get to the other files with the ":next" command. Note that a number of things that may be regarded as bugs by some, are in fact caused by a � Step 5 type :wq (write file and exit vim) Editing the Existing File. Step 1 type vim filename (edit the existing file named filename) Step 2 move around the file using h/j/k/l key or any appropriate command h Moves the cursor one character to the left. l Moves the cursor one character to the right. k Moves the cursor up one line

vim man page, n\)\{2} : find 2 successive lines starting with str " using rexexp memory in a global command display :g/gladiolli/# : display with line numbers (YOU WANT THIS!) MAKE IT EASY TO UPDATE/RELOAD _vimrc :nmap ,s :source $VIM/_ vimrc :nmap :lcd %:p:h : change to directory of current file :autocmd BufEnter * lcd %:p:h� To force vi/vim display line numbers, you need to set the number flag. To do so, edit a file named ~/.vimrc. If you are using old good vi text editor edit a file named ~/.exrc:

Best of VIM Tips, gVIM's Key Features zzapper, is the current line,; $ is the last line in the buffer,; 1 is the first line in the Given the above file, with the cursor on the first line, :m 3 will move it to below the third line: and many other Vim quirks (why would you use :wq to leave?), it is for buffers in Vim 3.0, and Vimscript and syntax highlighting in Vim 5.0. The following Vim lesson is an excerpt from “Vim and Vi Tips.” You can get in PDF in a couple of minutes! If you ever need to know the current line number, try these commands: Display the line number: type :#, :num, :.=, or press Ctrl+G. Display the number of lines in the current file: type := Display the line number and the total number of

Comments
  • I've never used vimscript, but this answer might help: stackoverflow.com/a/553257/778118
  • Don't use Google for that kind of thing. The answer is on your machine: :help functions.
  • Upon rereading the question, I realize you were trying to do this via vimscript. Sorry, my answer is not useful for that purpose.
  • Still, for someone who stumbled upon this from Google, this was the best answer for me
  • If the OP wants to use that number back into a vim script, neither 'statusline' nor CTRL-Gwill be helpful to him. line('$') will return the number of lines in the current buffer, and len(readfile(filename)) the number of lines in a given file.
  • title says nothing about script - perhaps it should be changed then
  • 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.
  • Changed the title :)