0

I want to search for a keyword, and replace a word in the line that is not a search word. I thought macros may be a good way to do it, but I am open to any methods.

I have something like,

blah blah
 check abc = 9 
blah
 check def VS 10 
 check hi and j = 20

when I have "check", I want to replace the = a few words down in the same line with VS. There are too many = in the file that are unrelated to it to directly search and replace. I want to do this on a large file. I made a macro that contains

  /check
   s/=/VS/gc

and thought to keep running it by putting a range, or repeat it with pressing a period. When the second search and replace does not always find =, the range seem to stop working after one attempt. In the same case, the period does not work either, because now it skips searching for the check.

1

I would combine it with the :g command:

:g/^\s*check\>/s/=/VS/

This selects all lines that has a "check" as the first word and then executes s/=/VS/ on this line. This will replace the first = with VS.

You can also use the substitution flags gc if you like to replase all equal signs and want to confirm every substitution:

:g/^\s*check\>/s/=/VS/gc

See :help :global.

| improve this answer | |
0
:%s/^\(\s*check[^=]\+\)=\(.*\)/\1VS\2/

transforms your sample data to

blah blah  
check abc VS 9  
blah  
check def VS 10  
check hi and j VS 20

that is any line containing check later followed by an = sign gets = changed to VS

Add c at the end of the command to have it stop at every occurrence for confirmation.

| improve this answer | |
0

The other answers have given suggestions for other approaches you could take, but your macro actually works fine. The problem you were having is that you cannot repeat a macro with .: this will instead repeat the last action within the macro. To play back the most recently played macro again, use @@.

There are a couple of small tweaks you could make to improve things:

  1. You can't really use c in a :substitute command within a macro, because you're the macro recording will also include your "answers" to the c prompt,

  2. As it stands, your macro will stop on every line that contains check, and display an error about the :substitute not finding the pattern. It's easy to get it to skip lines without an equals sign, though. Just update your search to check for this:

     /check.*=
    

Note also that the macro that you have recorded can't be replayed over a range. Or rather, it can, but it won't do what you want it to: every time the macro runs, it will search forwards for the next line that contains check, so if your range also includes lines that do not include check, you will be running the macro too many times, and will likely end up editing text outside of the range.

| improve this answer | |

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.