3

我正在查看可能具有多个连续相同行的文件。

有没有一种简单的方法可以跳到下一个不同的行?

或者,我希望能够折叠所有等于初始行的行,仅显示折叠的行数。

4

2 回答 2

7

您可以定义自己的折叠表达式:

首先设置 fdm:

:set fdm=expr

然后

:set foldexpr=getline(v:lnum)==#getline(v:lnum-1)?1:0

现在你可以通过输入zM, 来测试关闭所有折叠,如果你幸运的话 ^_^ 所有重复的行都被折叠了。

您可以键入zR以打开所有折叠。

如果它有效并且您经常打开此类文件,则可以将上述行放入您的 .vimrc.(au with ft) 如果只有一次工作,您可以将模式行写入该文件。

于 2013-03-29T21:41:47.553 回答
1

试试这个:

  :nmap <F2> "1y$<CR>/^\(<C-R>1$\)\@!<CR>

它将 F2 映射到:

  • 将当前行复制到寄存器 1
  • search for (and move to) the first line that does not match the contents of register 1

This seems to work well, unless the text of your copied line has escaped characters that will confuse the search regexp. This is because register 1 is just dropped into the search expression without escaping. This would be tricky to fix reliably, but for normal log files, it shouldn't be much of a problem.

Also: if you're not married to vim and just need to read the non-consecutively-duplicated lines of a file, the canonical UNIX way is:

uniq filename

If you want to be in vim but won't need to make changes to the file, try:

:%!uniq

(If you try the latter, be sure to exit without saving)

于 2013-03-29T23:14:01.793 回答