9

I'm reading vim help about the line function and here is an example I found:

:let failed = append(line('$'), "# THE END")

In my vimrc I also found line('.'). The documentation says:

line( {expr}) Number line nr of cursor, last line or mark

I tried to invoke it as

:echo :call line('$')

but this printed the following error:

E121: Undefined variable: :call
E15: Invalid expression: :call line('$')

What does '.' and '$' mean here and how to invoke it correctly?

statox
  • 49,782
  • 19
  • 148
  • 225
user3663882
  • 713
  • 1
  • 8
  • 11
  • 1
    What doc have you read? If you found :h line() the 3rd and 4th lines describes what . and $ means. – statox Sep 29 '16 at 09:44
  • @statox :h functions. Didn't think there were docs for each function, – user3663882 Sep 29 '16 at 10:25
  • One of the strength of Vim is that everything is documented you might be interested in reading this question – statox Sep 29 '16 at 10:46
  • 2
    The whole idea with calling functions syntax is: if you use the returned value, you don't need to use call; but, if you are just calling the function, then you need to use call. So, :echo line('$') or :call line('$'), but not :echo :call line('$'). – VanLaser Sep 29 '16 at 11:28

1 Answers1

14

In :help line all possible values of {expr} are explained:

.       the cursor position                                 
$       the last line in the current buffer                 
'x      position of mark x (if the mark is not set, 0 is returned)                                           
w0      first line visible in current window                
w$      last line visible in current window                 
v       In Visual mode: the start of the Visual area (the   
        cursor is the end).  When not in Visual mode        
        returns the cursor position.  Differs from '< in    
        that it's updated right away.

And to invoke that you just need:

:echo line('.')

call is for running functions:

function! func()
    echo line('.')
endfunction

If you would have something like that in your .vimrc, then you could use call like so: :call func()

grodzik
  • 4,588
  • 19
  • 27
  • For both '.' and '$' line() returns 1 for an empty file. It also returns 1 for a file that contains just a single newline character. Is there any way to tell the difference? – user98761 May 09 '22 at 17:32