1

I have a long script file that I need to run in an application. I generally create these by running a query against the application and then creating the script I need using VIM. Sometimes these can become very long (millions of lines long), but the product handles it just fine. It just takes time.

I run these in the product API through a terminal session. The problem with this is that I never know how far along it is in processing the script. I would like to add line numbers after a comment character. I can then compare what I see in the running script against the total number of lines and guess when it will complete.

The comment character is a # after which I can add whatever text I want. The script engine will ignore what follows a # character. I can easily select lines to place this on because the script I'm running has some blank lines between every set of about 15 commands. So roughly 15 actual lines of commands and then 2 blank lines and then 15 more lines and so on.

Here's the problem though. It seems that using a command like

g/^$/s//\#\=printf('%d',line('.'))/

generates an error. It doesn't matter what text I add, i get an error if the text I want to insert includes anything other than \=printf('%d',line('.')). As a result, I can't prefix this with the comment character #.

Is there some other syntax I can use to do this?

4
  • please note that somehow posting this some backslashes were dropped before the = character and before the # character. They show in the editor, but not the text of the question showing in the web page.
    – klamerus
    Jul 26, 2020 at 1:12
  • This can be solved if you use backticks (`) or indent with 4 spaces to mark code as such; I edited your post to fix it :-) Jul 26, 2020 at 5:54
  • 3
    As for your question, \= must be at the start of the substitution pattern; so putting a \# in front of that isn't valid. I think perhaps \=printf('# %d',line('.')) might do what you want? It might be helpful if you could post a simple "before" and "after" example, because I'm not sure if I parsed your requirements from the text exactly 😅 Jul 26, 2020 at 5:59
  • Also, welcome to Vi and Vim! I think Martin is correct re: the solution to your problem.
    – D. Ben Knoble
    Jul 26, 2020 at 13:39

1 Answer 1

1

Turning Martin's comment into an answer, you can't have both a literal replacement and \=, but you can use string concatenation or printf to combine them:

global/^$/substitute//\=printf('# %d', line('.'))/

In fact, you could even do

global/^$/delete | put ="printf('# %d', line('.'))"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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