How can I format JS code in Vim?

前端 未结 7 1883
半阙折子戏
半阙折子戏 2020-12-14 08:19

I have this bit of JavaScript...

 15   $(\'.ajax_edit_address\').each(function() {
 16     $(this).ajaxForm({
 17       target: $(this).parents(\'table.addre         


        
7条回答
  •  情话喂你
    2020-12-14 09:08

    Another alternative that do not need to configure anything inside vim is to run the format command manually at save like:

    :w !js-beautify --stdin >%
    

    After saving in this way the vim editor will ask you to reload the current file content:

    W12: Warning: File "src/static/js/main.js" has changed and the buffer was changed in Vim as well
    See ":help W12" for more info.
    [O]K, (L)oad File: 
    

    This works like the :w sudo tee % command used to save a file that you modified without privilege.

    The command uses the standard input(STDIN) and write it to a variable file descriptor % used as source of current file.

    PS: of course you need to install the js-beautify.

    pip install jsbeautifier
    

提交回复
热议问题