1

I'm attempting to write a Vim macro that will allow me to replace a single variable of a single service across multiple Icinga configuration files. Services are syntactically very similar so I constructed a multi-tier search before replacing the variable.

Writing the Macro:

qd
/SERVICE_2
/max_check_attempts
:s/5/10/g
q

Running the Macro:

vim config-file-*.cfg 
:argdo @d 
:wqall

Executing this macro however results in only a single file being written, the first one in the args list, whereas I want to make the change to multiple config files (config-file-1.cfg, config-file-2.cfg, etc.). Where am I going wrong? and how do I resolve this issue?

Further Details

Vim Register for @d:

"d   /SERVICE_2^M/max_check_attempts^M:s/5/10g<80>kb/g^M

Args of vim config-file-*.cfg:

[config-file-10.cfg] config-file-1.cfg config-file-2.cfg config-file-3.cfg confi
g-file-4.cfg config-file-5.cfg config-file-6.cfg config-file-7.cfg config-file-8
.cfg config-file-9.cfg

Example contents of a config file:

define service{
        use                             generic-service 
        host_name                       host1
        service_description             SERVICE_1
        is_volatile                     0
        check_period                    mainperiod
        max_check_attempts              5
        normal_check_interval           4
        retry_check_interval            30
        contact_groups                  mycontacts
        notification_interval           15
        notification_period             mainperiod
        notification_options            w,u,c,r
        check_command                   check_service_1
}

define service{
        use                             generic-service
        host_name                       host1
        service_description             SERVICE_2
        is_volatile                     0
        check_period                    mainperiod
        max_check_attempts              5
        normal_check_interval           13
        retry_check_interval            7
        contact_groups                  mycontacts
        notification_interval           15
        notification_period             mainperiod
        notification_options            w,u,c,r
        check_command                   check_service_2
}

define service{
        use                             generic-service    
        host_name                       host1
        service_description             SERVICE_3
        is_volatile                     0
        check_period                    mainperiod
        max_check_attempts              5
        normal_check_interval           7
        retry_check_interval            9
        contact_groups                  mycontacts
        notification_interval           15
        notification_period             mainperiod
        notification_options            w,u,c,r
        check_command                   check_service_3
}

Thanks in advance for any help offered as well as any quality-of-life and best practices suggestions.

  • 1
    Not sure why saves aren't happening but I'd consider doing away with the macro and just putting the command on the argdo line: :argdo /SERVICE_2/;/max_check_attempts/ s/5/10/g. The /foo/;/bar/ notation at the start means "go to first occurrence of foo and then the first occurrence of bar following foo"...I think that's what you want to do. – B Layer Mar 23 at 15:24
  • A matter of personal taste is the use of macros here. You could also use a single substitute command: %s/\(SERVICE_2\_.\{-}max_check_attempts\s*\)\d*/\110/. It uses \_. to match everything including linebreaks, grouping into an atom \( \) and \{-} the non-greedy version of *. – Hotschke Mar 24 at 7:49
2

:argdo @d doesn’t act like executing @d in every buffer in the arg list: it instead runs :@d in each buffer. It doesn’t execute the contents of the d register as a macro: it executes them as an Ex command. See :help argdo and :help :@.

Try :argdo norm!@d instead.

| 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.