Vim Tips Wiki
Advertisement
Tip 578 Printable Monobook Previous Next

created October 5, 2003 · complexity intermediate · author duvell · version 6.0


I was recently using sed to pull out multi-line fields with sed when I wondered if I could specify a range using two search patterns in Vim as I can in sed. Sure enough it works.

Here is a contrived example. Suppose I had a Vim script I was editing and I want to comment out the function declaration of a function named My_func. Instead of searching for it, marking the range and then adding a comment to the start of the line, the following command will work:

:/^ *function *My_funct\>/,/^ *endfunction/s/^/" /

The range is specified by two patterns. For the start of the range I look for the line which contains function My_funct. I added the \> end of word delimeter to the pattern in case I had other functions that had names beginning with My_func.

The end of the range will be the first occurrence of the second pattern, /^ *endfunction/ starting from where the first pattern was matched.

The two patterns are separated with a comma as any range would be and the command to perform on the range follows. In this case a substitution, s/^/: / but it could be any command.

Comments

Advertisement