Skip to content

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?

  1. To turn off highlighting until the next search: :noh/:nohl/:nohls/:nohlsearch.
  2. Or turn off highlighting completely: :set nohlsearch; type :set hlsearch to turn on again.
  3. 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 key. Since hitting is a very common and intuitive situtation in Vim.

" noh - no highlight
map <esc> :noh <CR>

Clear search highlight - VimTricks

incrementally highlights pattern#

incsearch

incsearch.vim incrementally highlights ALL pattern matches unlike default 'incsearch'.

Search across multiple lines#

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

vim下正则表达式的非贪婪匹配
VIM中正则的非贪婪匹配

执行 git merge origin/master 后,想删除本地 ours HEAD 部分,采用 theirs MERGE_HEAD:

首先查找所有 conflict 的 ours 部分:

/^<<<<<<< HEAD\_$\_.\{-}\_^=======\_$

替换为空行:

:%s/^<<<<<<< HEAD\_$\_.\{-}\_^=======\_$//gc

但这并不是我们想要的,空行还是非预期占位一行。

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/^>>>>>>> origin\/master$/d

但是对于跨行匹配的结果,:g 只删除了第1行?

:g/^<<<<<<< HEAD\_$\_.\{-}\_^=======\_$/d

global 命令先扫描 [range] 界定的行,然后对每个匹配的行进行标记 (对于一个跨行的匹配,则只针对开始的一行)。

另一种方法:基于 gn 选中 search pattern 命中高亮部分(select text highlighted by search),再执行删除。

先执行查找:

/^<<<<<<< HEAD\_$\_.\{-}\_^=======\_$

再执行 gn$d,然后重复 gn$d

gn 后如果不添加 $,执行删除后换行分隔符将占位一行。

参考 VIM提取/删除匹配行的方法,实现 KeepLines(pattern) 函数。

对于大型的文本处理,建议使用 sed/awk 命令。

其他方案

  1. vscode 正则搜索
  2. sed/awk

Comments