Search code examples
regexvimvim-plugin

How can filter some multiline statement in vim


I have vim-script function call ScreenShellSend("some string") and I would like to be able to filter multiple lines to feed this function with a correct string.

How can I go from, for example, this:

//@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};

To:

anExampleOfFunction:{[x;y] x: doing some stuff; :y; }; someVariable: 5; anotherFunction:{[x] 2*x};

Solution

  • You can use the following substitute command to achieve your goal:

    :%s`\(//.\+\)\?\n``
    

    This removes comments and newlines.

    For your example, it will give you the following result:

    anExampleOfFunction:{[x;y]    x: doing some stuff;         :y; }; someVariable: 5;  anotherFunction:{[x] 2*x};
    

    Edit:

    Here is a function that does the same (except it will work with its argument instead):

    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