1

I know that there is a function col({expr}), which returns the column position given with {expr}.

For example, col("$") gives me the column position of the end of the cursor line.

So I thought col("^") can give me the column position of the first non-whitespace character of the cursor line. But col("^") always return 0.

if xxx
    hello|<here is the cursor> world
endif

In the example above, when I execute :echo col("^"), I always get a 0, instead of 4.

Why? BTW, 4 spaces or a tab before the hello make no difference.

And if col("^") can't give me the column position of the first non-whitespace character, how could I get it?

1

So I thought col("^") can give me the column position of the first non-whitespace character of the cursor line. But col("^") always return 0.

That's a fair assumption, but '^' is not documented at :h col():

col({expr})     The result is a Number, which is the byte index of the column
                position given with {expr}.  The accepted positions are:
                    .       the cursor position
                    $       the end of the cursor line (the result is the
                            number of bytes in the cursor line plus one)
                    'x      position of mark x (if the mark is not set, 0 is
                            returned)
                    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.

IOW, passing '^' to col() is an error, and the latter returns 0:

The first column is 1.  0 is returned for an error.

And if col("^") can't give me the column position of the first non-whitespace character, how could I get it?

Try this:

echo getline('.')->match('^\s*\zs\S') + 1

For more info, see:

1

So I thought col("^") can give me the column position of the first non-whitespace character of the cursor line.

For me it's hard to imagine why you think so. ^ is a normal mode command, it's nowhere said it would be accepted by col().

how could I get it?

Well, the easiest way is indent('.') + 1, but, in fact, it gives virtcol(), not col(). Sometimes it's good, sometimes not. So if you need col() for real, you have to invoke match(getline('.'), '\S') + 1.

5
  • Well, I often use ^ and $ together. That's why I thought col("^") and col("$") should work.
    – Yves
    Oct 13 '20 at 6:57
  • @Yves Accepted parameters are listed under :h col() as usual. If you believe ^ must be there, why not also _, and g^, and g$ and myriad of others?
    – Matt
    Oct 13 '20 at 7:02
  • Because I haven't noticed that the list is the ONLY accepted expr...
    – Yves
    Oct 13 '20 at 7:11
  • 1
    @Yves In IT sphere (as in a few others) pessimism prevails. Every time one sees docs claiming "that thing works", he should read "it's the ONLY thing that MAYBE working" instead.
    – Matt
    Oct 13 '20 at 7:18
  • 1
    @Matt As an IT professional let me just say...LMAO!
    – B Layer
    Oct 13 '20 at 20:33

This site is temporarily in read only mode and not accepting new answers.

Not the answer you're looking for? Browse other questions tagged .