vim匹配
本文简单梳理了 vim 搜索匹配模式,相关议题 :h pattern
。
clear search highlighting#
highlight - Vim clear last search highlighting - Stack Overflow
After doing a search in Vim, I get all the occurrences highlighted. How can I disable that? I now do another search for something gibberish that can't be found. Is there a way to just temporarily disable the highlight and then re-enable it when needed again?
- To turn off highlighting until the next search:
:noh
/:nohl
/:nohls
/:nohlsearch
. - Or turn off highlighting completely:
:set nohlsearch
; type:set hlsearch
to turn on again. - From the VIM Documentation,to clear the last used search pattern:
:let @/ = ""
Clear Search Highlight in Vim — tech.serhatteker.com
I personally use 'temporarily disable' with key mapping. I mapped :noh to
Clear search highlight - VimTricks
incrementally highlights pattern#
incsearch.vim
incrementally highlights ALL pattern matches unlike default 'incsearch'.
Search across multiple lines#
How to substitute text across line breaks?
How to do a multiline search in vim
How to search and delete literal text spanning multiple lines?
Multiline Search and Replace
Multi-line regex support in Vim
The search /^abcd
finds abcd
at the beginning of a line, and /abcd$
finds abcd at the end of a line.
However, in /abcd^efgh
and /abcd$efgh
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.
meta | 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 newline |
shortest match first#
相关议题::h non-greedy
执行 git merge origin/master
后,想删除本地 ours HEAD 部分,采用 theirs MERGE_HEAD:
首先查找所有 conflict 的 ours 部分:
替换为空行:
但这并不是我们想要的,空行还是非预期占位一行。
delete all patterns#
Delete all lines containing a pattern
How to delete a multi-line match?
Efficient way to delete line containing certain text in vim with prompt
:g
可以删除 theirs 部分的单行结尾标记:
但是对于跨行匹配的结果,:g
只删除了第1行?
global 命令先扫描
[range]
界定的行,然后对每个匹配的行进行标记 (对于一个跨行的匹配,则只针对开始的一行)。
另一种方法:基于 gn
选中 search pattern 命中高亮部分(select text highlighted by search),再执行删除。
先执行查找:
再执行 gn$d
,然后重复 gn$d
。
gn
后如果不添加$
,执行删除后换行分隔符将占位一行。
参考 VIM提取/删除匹配行的方法,实现 KeepLines(pattern) 函数。
对于大型的文本处理,建议使用 sed/awk 命令。
其他方案
- vscode 正则搜索
- sed/awk