Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In vim, I can find the matching if statement and prepend the appropriate comment symbol. (e.g. %s/.alarm./#\0/g), but then I am left with dangling endifs that I have to find manually. I could simplify the process by using macro, but I could I achieve this with sed (more specifically one in vim)?

share|improve this question
up vote 2 down vote accepted

The problem with a simple regular expression is dealing with nested conditionals; you cannot simply search for the next endif and comment it out. Fortunately, the matchit plugin that comes with Vim has this problem solved (for many filetypes); when on the if, the % mapping will take you to the corresponding endif.

Combine that with a plugin like NERDCommenter, and you can easily build a mapping for it:

:nmap <Leader>cif <Leader>cc%<Leader>cc
share|improve this answer
Thank you. I never thought about using those plugins that way! – Forethinker Apr 14 '13 at 10:48

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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