一种替代@sehe方法的方式不需要使用临时文件:
funct! DiffTwoTexts(text1, text2)
new
put =a:text1
normal ggdd
diffthis
new
put =a:text2
normal ggdd
diffthis
endfunct
funct! DiffTwoLines(line1, line2)
let text1 = getline(a:line1)
let text2 = getline(a:line2)
call DiffTwoTexts(text1, text2)
endfunct
comma! DiffWithNext call DiffTwoLines('.', line('.') + 1)
由于所有内容都在同一行上,因此这篇文章仍然很难阅读,所以我进行了修改:
funct! EvalTextPreprocessor(expr, text)
let text = a:text
return eval(a:expr)
endfunct
comma! -nargs=1 DiffWithNextPre call DiffTwoTexts(
\ EvalTextPreprocessor(<q-args>, getline('.')),
\ EvalTextPreprocessor(<q-args>, getline(line('.') + 1)))
这个新命令接受一个vimscript表达式作为参数,在表达式中,变量text
指的是正在被预处理的行。因此,您可以调用例如:
DiffWithNextPre split(text, '[(,)]\zs')
对于您的样本数据,这将提供两个缓冲区。
AVeryLongReturnType* MyLongClassName:hasAnEvenLongerFunction(
That *is,
Overloaded *with,
Multiple *different,
Parameter *lists)
;
并且
AVeryLongReturnType* MyLongClassName:hasAnEvenLongerFunction(
That *is,
Overloaded *with,
Multiple *different,
Parameter *1ists)
;
只有以Parameter
开头的行才会被突出显示。
您甚至可以从那里开始构建,创建一个命令。
comma! DiffTwoCFunctionSigs DiffWithNextPre split(text, '[(,)]\s*\zs')
请注意,我稍微修改了正则表达式,以便它将在行尾保留尾随空格。您可以通过将
\s*
移动到
\zs
之后来忽略它们。如果您不熟悉vim特定的RE原子,请参阅
:help /\zs
。
另一个好处是使该命令接受一个范围(请参阅
:help command-range
),您可以通过将范围的第一行与最后一行进行比较来使用它。因此,您只需要从第一行到第二行进行可视选择并调用命令即可。
:diffthis
(:difft
) 命令,它们将被进行差异比较。 - Evgeni Sergeev