Setting formatting rules in Vim

I'm not Vim noob, but I'm not a professional either. If I have a segment, in this case javascript, such code

function foo(a,b,c,d) { if (one||two) { if (rhyme||reason) { return true; } return false; } } 

and I will format it ( gg=G ), I get the following code:

 function foo(a,b,c,d) { if (one||two) { if (rhyme||reason) { return true; } return false; } } 

It is more aesthetically pleasing. However, if instead I wanted lower (or if I wanted to convert the above result into the following result):

 function foo( a, b, c, d ) { if ( one || two ) { if ( rhyme || reason ) { return true; } return false; } } 

Is there a way to instruct Vim how to parse the code? I can use a hack with regular expressions to search for brackets and spaces, but it falls apart if we consider all the operators ( !,%,*,-,+,=,/ )

+4
source share
2 answers

Try this vim plugin: https://github.com/Chiel92/vim-autoformat

A click of a button and you're done. It uses gq and installs external formatted programs for proper use.

Basically, if you install this plugin along with einars js-beautify, it can format javascript out of the box.

+4
source

A few different comments:

+5
source

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


All Articles