WCGPR2
WCGPR2

Reputation: 55

How to count how many specific characters before cursor on the current line using vim?

In vim, would it be possible to count the number of a specific pattern before the current cursor position?

foo1*foo2*foo3*foo4*foo5,...
bar1*bar2*bar3*[cursor]bar4*bar5,...

If [cursor] is where I'm interested in, is there any way I can quickly count the number of asterix before this on this particular line (line 2), in which case the result should be 3. This way I know the position of this N is 4.

In a file with fooN and barN where N is big, I would like a fast & simple way to determine the position of this (and of course, the actual data doesn't have N).

Upvotes: 2

Views: 389

Answers (1)

XPlatformer
XPlatformer

Reputation: 1228

:echo count(getline('.')[0:getpos('.')[2]-1], '*')

Upvotes: 2

Related Questions