Vim how to do: g work correctly with zs matches

I have this at the top of my file:

var server = require('http');
var watchr = require('watchr');
var io = require('socket.io');
var fs = require('fs');

I want to convert this garbage to the corresponding var list:

var server = require('http'),
    watchr = require('watchr'),
    io = require('socket.io')(server),
    fs = require('fs');

So, my first step is to find all the ads varin this paragraph , except the first , and put them off. Therefore, I am looking for every word varthat precedes another varin the previous line.

Here's a regex that for some reason works:

\vvar\_.{-}\zs<(var)@>

Explanation:

\v          Very magic mode (make all special characters special)
var         The word var
\_          Spanning multiple lines
.           Any charaacter
{-}         Non greedy
\zs         Actually start higlighting our search here
<           A word boundary
(var)       The second occurance of the word var
@>          Treat the previous thing (var) as the whole match. Otherwise
                 Vim is too stupid to match consecutive lines and only
                 matches every other line

Now that I hit nin my file, Vim correctly jumps to every statement var except the first one. Hooray!

:g , ( , , n )

v i p :

'<,'>g//norm>>

:

    var server = require('http');
    var watchr = require('watchr');
    var io = require('socket.io');
    var fs = require('fs');

? ? g zs @> - Vem regex, ?

+4
1

:global , :

:g/^var/+norm >>

:

var server = require('http');
    var watchr = require('watchr');
    var io = require('socket.io');
    var fs = require('fs');

, :

  • :

    vipJ or :'{,'}j<CR>
    

    :

    var server = require('http'); var watchr = require('watchr'); var io = require('socket.io'); var fs = require('fs');
    
  • :

    :s/; var /,\r    /g
    

    :

    var server = require('http'),
        watchr = require('watchr'),
        io = require('socket.io'),
        fs = require('fs');
    
+3

Source: https://habr.com/ru/post/1547367/


All Articles