History
Article Edit this page Discussion

Specify Range with search patterns

From Vim Tips Wiki

(Redirected from VimTip578)
Jump to: navigation, search

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

[edit] Comments

Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Doctor Who
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Mystery Science Theater 3000
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Specify Range with search patterns"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation