0

How can I get a numeric return value from an Ex command. For example, if I want to set a variable to the number of times a pattern occurs in a buffer. I tried this:

:let answer=exe('%s/xyz//n')

but I get the error "Unknown function exe".

  • I believe I've answered both your primary and secondary questions (for the latter Christian and I agree that short of a complex/slow solution the parse of :s///n is your best bet). If you disagree please indicate what I've missed. If you agree please accept. Posts that are unresolved+stagnant detract from this site. Another of your questions I've answered is also in this state. In fact, I notice that out of your last 27 questions you've only accepted 2...that's kind of extreme. Not trying to be a PITA...just like to see this site, my personal favorite, thrive. Cheers. – B Layer Feb 3 '18 at 11:19
  • @BLayer I see, so you think my question is good enough to be worth your time to answer, but my question is not good enough to upvote? Personally I think it is pretty bogus for people to upvote answers, but not the questions that spurred the answers. I will be happy to start voting for answers, when I see people start upvoting questions. – Tyler Durden Feb 3 '18 at 11:51
  • 1
    I didn't ask for an upvote I asked if you could accept answers per the way this site is meant to operate. Let me see if I have this right. Someone could see a question from you, spend a bunch of time researching and writing a good answer that turns out to be useful to you, and you can't bring yourself to accept their answer because they didn't upvote? How childish. (Maybe they thought your question wasn't generally useful to the community. Or maybe they just forgot to vote.) If everyone had petty tit-for-tat requirements like this the site would not work at all. – B Layer Feb 3 '18 at 17:08
3

The function is execute() not exe(). Command names can be abbreviated, not functions.

The answer to your question "how can I get a numeric return value from a command" is "call a command that returns a number". ;) No seriously. Your assignment to a variable is valid. But your example won't work as execute() returns the string that is returned by the :s call. And in fact execute() always returns strings so you'll never get a number from it. But a numerical string will often do and if you really need a number you can wrap a command/function that returns a string in str2nr().

I think that answers your question, but turning to your example we can get a numerical string answer as follows.

The string returned by :s is of the form X matches on Y lines so if you want to extract the number of matches you can use this:

let answer=substitute(execute('%s/xyz//n'), ' .*', '', 'g')

It just strips off everything from the first space on leaving the first number. A bit hacky.

  • That seems like kind of a hack. Is there a more direct way? I thought exe and exec and execute where synonyms. – Tyler Durden Jan 27 '18 at 3:50
  • You are asking two questions here. The one you directly ask is "How do I get a numeric return value from an Ex command?" and that's what I've answered (see my update...it's more explicit in answering that question). Then you are peripherally asking "How do I get the number of occurrences of a pattern?" As an extra I gave a hack to do it with your command but maybe you could open a new question for that since it's quite different from the other, much more general question. – B Layer Jan 27 '18 at 7:31
  • That being said, I did look for another way to do it and every short, out-of-the-box solution (i.e. not a custom function or plugin) I found parses :s///n some way or another. Nothing less hacky. – B Layer Jan 27 '18 at 8:48
  • The correct way would be to count yourself using search() function. However this might be slow so one usually just fetches the output of :s and works with the capture (note you'll have to respect &report option and you have to take care of translations) – Christian Brabandt Jan 27 '18 at 9:12
  • @ChristianBrabandt Thanks for confirming. I was thinking search() as I pondered a "custom function". But I'm not seeing any effect from &report with the n flag. With default value of 2 I'll still see 1 match on 1 line, for example. Am I misunderstanding you? – B Layer Jan 27 '18 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.