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?

4
  • 1
    What doc have you read? If you found :h line() the 3rd and 4th lines describes what . and $ means.
    – statox
    Commented Sep 29, 2016 at 9:44
  • @statox :h functions. Didn't think there were docs for each function, Commented Sep 29, 2016 at 10:25
  • One of the strength of Vim is that everything is documented you might be interested in reading this question
    – statox
    Commented Sep 29, 2016 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
    Commented Sep 29, 2016 at 11:28

1 Answer 1

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

1
  • 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
    Commented May 9, 2022 at 17:32

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.