How do I fix incorrect inline Javascript indentation in Vim?

后端 未结 9 1584
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 15:09

I can\'t seem to get inline Javascript indenting properly in Vim. Consider the following:

  $(document).ready(function() {

  // Closing brace correctly inde         


        
9条回答
  •  情深已故
    2020-12-12 15:26

    You don't have to install plugins specialised for Javascript, you can learn the built-in Vim options for indentation. Vim has quite a few options, and some of the indenting styles like cindent, smartindent and indentexpr have options of their own.

    To check whether you are using cindent or smartindent or indentexpr, run:

    :set cindent?
    :set smartindent?
    :set indentexpr?
    

    Despite the name, cindent doesn't just apply to C programs, it can apply to a bunch of programming languages that share roughly the same syntax, including Javascript. Have a look at :help C-indenting for documentation about this. You can adjust the settings particularly with a line like this one, see :help 'cinoptions' and :help cinoptions-values. Here's an example configuration:

    :au FileType js,javascript setlocal shiftwidth=2 softtabstop=2 cinoptions=j1,J1,(1s " see help cino-j cino-J cino-(
    

提交回复
热议问题