如何过滤 vim 中的一些多行语句
How can filter some multiline statement in vim
我有 vim-script 函数调用 ScreenShellSend("some string")
,我希望能够过滤多行以便为该函数提供正确的字符串。
我怎样才能从这里开始,例如:
//@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};
收件人:
anExampleOfFunction:{[x;y] x: doing some stuff; :y; }; someVariable: 5; anotherFunction:{[x] 2*x};
您可以使用以下 substitute
命令来实现您的目标:
:%s`\(//.\+\)\?\n``
这将删除注释和换行符。
对于您的示例,它将为您提供以下结果:
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
我有 vim-script 函数调用 ScreenShellSend("some string")
,我希望能够过滤多行以便为该函数提供正确的字符串。
我怎样才能从这里开始,例如:
//@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};
收件人:
anExampleOfFunction:{[x;y] x: doing some stuff; :y; }; someVariable: 5; anotherFunction:{[x] 2*x};
您可以使用以下 substitute
命令来实现您的目标:
:%s`\(//.\+\)\?\n``
这将删除注释和换行符。
对于您的示例,它将为您提供以下结果:
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