Search across multiple lines using regular expression in VIM
来源:互联网 发布:php开源文档管理 编辑:程序博客网 时间:2024/05/21 10:54
Search across multiple lines
(come from http://vim.wikia.com/wiki/Search_across_multiple_lines)
created 2002 · complexity intermediate · version 6.0
Vim can search for text that spans multiple lines. For example, the search /hello\_sworld
finds "hello world" in a single line, and also finds "hello" ending one line, with "world" starting the next line. In a search,\s
finds space or tab, while\_s
finds newline or space or tab: an underscore adds a newline to any character class.
This tip shows how to search over multiple lines, and presents a useful command so entering:S hello world
finds "hello" followed by "world" separated by spaces or tabs or newlines, and:S! hello world
allows any non-word characters, including newlines, between the words.
Patterns including end-of-line
The search /^abc
finds abc
at the beginning of a line, and/abc$
findsabc
at the end of a line. However, in /abc^def
and /abc$def
the ^
and $
are just ordinary characters with no special meaning. By contrast, each of the following has a special meaning anywhere in a search pattern.
\n
a newline character (line ending)\_s
a whitespace (space or tab) or newline character\_^
the beginning of a line (zero width)\_$
the end of a line (zero width)\_.
any character including a newlineExample searches:
/abc\n*def
- Finds
abc
followed by zero or more newlines thendef
. - Finds
abcdef
orabc
followed by blank lines anddef
. - The blank lines have to be empty (no space or tab characters).
/abc\_s*def
- Finds
abc
followed by any whitespace or newlines thendef
. - Finds
abcdef
orabc
followed by blank lines anddef
. - The blank lines can contain any number of space or tab characters.
- There may be whitespace after
abc
or beforedef
.
/abc\_$\_s*def
- Finds
abc
at end-of-line followed by any whitespace or newlines thendef
. - There must be no characters (other than a newline) following
abc
. - There can be any number of space, tab or newline characters before
def
.
/abc\_s*\_^def
- Finds
abc
followed by any whitespace or newlines thendef
wheredef
begins a line. - There must be no characters (other than a newline) before
def
. - There can be any number of space, tab or newline characters after
abc
.
/abc\_$def
- Finds nothing because
\_$
is "zero width" so the search is looking forabcdef
whereabc
is also at end-of-line (which cannot occur).
/abc\_^def
- Finds nothing because
\_^
is "zero width" so the search is looking forabcdef
wheredef
is also at beginning-of-line (which cannot occur).
/abc\_.\{-}def
- Finds
abc
followed by any characters or newlines (as few as possible) thendef
. - Finds
abcdef
orabc
followed by any characters thendef
.
Searching for multiline HTML comments
It is common for comments in HTML documents to span several lines:
<!-- This comment covers two lines. -->
The following search finds any HTML comment:
/<!--\_.\{-}-->
The atom \_.
finds any character including end-of-line. The multi\{-}
matches as few as possible (stopping at the first "-->
"; the multi*
is too greedy and would stop at the last occurrence).
Syntax highlighting may be not be accurate, particularly with long comments. The following command will improve the accuracy when jumping in the file, but may be slower (:help :syn-sync):
:syntax sync fromstart
Searching over multiple lines
A pattern can find any specified characters, for example, [aeiou]
matches 'a' or 'e' or 'i' or 'o' or 'u'. In addition, Vim defines several character classes. For example,\a
is[A-Za-z]
(matches any alphabetic character), and \A
is [^A-Za-z]
(opposite of \a
; matches any non-alphabetic character).:help /\a
An underscore can be used to extend a character class to include a newline (end of line). For example, searching for\_[aeiou]
finds a newline or a vowel, so\_[aeiou]\+
matches any sequence of vowels, even a sequence spanning multiple lines. Similarly,\_a\+
matches any sequence of alphabetic characters, even when spanning multiple lines.
The following search pattern finds "hello world" where any non-alphabetic characters separate the words:
hello\_[^a-zA-Z]*world
The above pattern (which is equivalent to hello\_A*world
) matches "helloworld", and "hello? ... world", and similar strings, even if "hello" is on one line and "world" is on a following line.
Searching over multiple lines with a user command
The script below defines the command :S
that will search for a phrase, even when the words are on different lines. Examples:
:S hello world
- Searches for "hello" followed by "world", separated by whitespace including newlines.
:S! hello world
- Searches for "hello" followed by "world", separated by any non-word characters (whitespace, newlines, punctuation).
- Finds, for example, "hello, world" and "hello+world" and "hello ... world". The words can be on different lines.
After entering the command, press n
or N
to search for the next or previous occurrence.
Put the following in your vimrc (or in file searchmultiline.vim
in your plugin directory):
" Search for the ... arguments separated with whitespace (if no '!')," or with non-word characters (if '!' added to command).function! SearchMultiLine(bang, ...) if a:0 > 0 let sep = (a:bang) ? '\_W\+' : '\_s\+' let @/ = join(a:000, sep) endifendfunctioncommand! -bang -nargs=* -complete=tag S call SearchMultiLine(<bang>0, <f-args>)|normal! /<C-R>/<CR>
- Search across multiple lines using regular expression in VIM
- CASE: Match multiple lines of text using Regular Expression.
- More about using regular expression in notepad++
- A Regular Expression Wrapper Using ATL in C++
- regular expression multiple line match
- REGULAR EXPRESSION IN VBSCRIPT
- Regular Expression in PHP
- Regular Expression in JavaScript
- Latex: Expression under summation on multiple lines
- Inserting text in multiple lines
- Regular expression in find command
- Use regular expression in notepad++
- Regular Expression Matching in Java
- Regular expression examples in javascript
- comment/uncomment lines in Vim
- 多正则表达式匹配(Multiple Regular Expression Matching)
- use regular expression in the oracle sqlplus
- How to use Regular Expression in LotusScript
- Windows XP使用技巧大全
- ps 使用
- JavaScript_A Beginner's Guide - Using an external JavaScript file - 09/20/2012
- jackrabbit version
- 录像方向设置
- Search across multiple lines using regular expression in VIM
- Java 位运算 64位long数据 转成2个 32位 int数据 及 回加成原数据
- Handler使用总结
- java finalize test
- 12个有趣的C语言面试题
- 优秀程序员的10种特质
- 获取国家气象局数据
- linux-kernel 3.5.3Tcp系统调用,源码分析4-inet_init
- “白色”程序员(猿)节