Regex 如何在vim中过滤一些多行语句
我有vim脚本函数调用Regex 如何在vim中过滤一些多行语句,regex,vim,vim-plugin,Regex,Vim,Vim Plugin,我有vim脚本函数调用ScreenShellSend(“一些字符串”),我希望能够过滤多行代码,为这个函数提供正确的字符串 例如,我如何从以下方面着手: //@brief: an example => TO LINE IS REMOVED anExampleOfFunction:{[x;y] x: doing some stuff; //a comment => after // is removed //a comment => this is removed
ScreenShellSend(“一些字符串”)
,我希望能够过滤多行代码,为这个函数提供正确的字符串
例如,我如何从以下方面着手:
//@brief: an example => TO LINE IS REMOVED
anExampleOfFunction:{[x;y]
x: doing some stuff; //a comment => after // is removed
//a comment => this is removed
:y;
};
someVariable: 5;
//another comment => this is removed
anotherFunction:{[x] 2*x};
致:
您可以使用以下
substitute
命令来实现您的目标:
:%s`\(//.\+\)\?\n``
这将删除注释和换行符
例如,它将为您提供以下结果:
anExampleOfFunction:{[x;y] x: doing some stuff; :y; }; someVariable: 5; anotherFunction:{[x] 2*x};
编辑:
下面是一个函数,其作用与此相同(除了它将使用其参数):
您还可以为原始文本和预期文本添加示例,而不是单独提供映射或命令。因此,我很容易理解您想做什么并回答您的问题。您可以转到每一行,按
f/
查找注释,然后按df$
删除,直到结束。然后按J键将其与下一行连接。按3J将把当前行与下两行连接起来,例如OK,但这并不是我所要求的,我想要一个vim脚本函数,所以基本上我将在脚本中调用该函数,如:your_函数(getline)(“'我编辑了我的答案,以包括一个按您要求工作的函数。
anExampleOfFunction:{[x;y] x: doing some stuff; :y; }; someVariable: 5; anotherFunction:{[x] 2*x};
function! Format(lines)
let lines = []
for line in a:lines
let new_line = substitute(line, '//.*', '', '')
call add(lines, new_line)
endfor
return join(lines)
endfunction