How to fix javascript invalid indent in Vim?

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

$(document).ready(function() { // Closing brace correctly indented $("input").focus(function() { $(this).closest("li").addClass("cur-focus"); }); // <-- I had to manually unindent this // Closing brace incorrectly indented $("input").blur(function() { $(this).closest("li").removeClass("cur-focus"); }); // <-- This is what it does by default. Argh! }); 

Vim seems to insist on automatically backing away from the closing brace shown in the second case. It does the same if I re-back the whole file. How to get it automatically indented using the more standard JS indentation style seen in the first case?

+62
javascript vim
Mar 06 '09 at 20:13
source share
8 answers

Use JavaScript Indent: A Javascript indenter (HTML indentation enabled) by Preston Koprivica. Thank you for the oligophrenic heads-up - give him a vote.

+80
Mar 06 '09 at 20:47
source share

The most complete and error-free indentation of the javascript script is the one that was Preston Koprivica . The so-called OOP script that is in the proposed answer has serious errors and does not have the correct indent code that has square brackets.

+84
Nov 15 '10 at 10:16
source share

The scripts mentioned above do not format the closure syntax, which is often used correctly in jQuery:

 $(function() { // only one level of indentation, not two }); 

This script works better for me: http://www.vim.org/scripts/script.php?script_id=2765

+17
Jul 20 '11 at 11:30
source share

Most of these answers relate to 2009 and, frankly, are outdated.

vim-javascript is much more recent and updated than the Preston script.

Installation is a little more complicated if you have not started using Vundle , but it does not seem to suffer from alternatives issues.

+10
Dec 09 '13 at 18:41
source share

maybe some combination of these options should be in your VIMRC file.

 syntax on set syn=auto set showmatch filetype on filetype plugin on filetype indent on set tabstop=4 set softtabstop=4 set shiftwidth=4 set expandtab 
+5
Mar 06 '09 at 20:35
source share

I had the same problem. This is the best of all Javascript indentation scripts:

http://www.vim.org/scripts/script.php?script_id=1840

This requires the IndentAnything plugin

http://www.vim.org/scripts/script.php?script_id=1839

As an added bonus, I wrote this indentation script that will make Javascript blocks pretty pretty. It uses the default html indenter by default (and IndentAnything when it is in a Javascript block)

http://gist.github.com/371902

+3
Apr 20 2018-10-10T00:
source share

If someone comes here, check out vim-javascript on pangloss at https://github.com/pangloss/vim-javascript , which has helped me so far, i.e. Vim 7.4. And the above solutions from oligophrenics and Charles Rooper did not.

+1
Apr 29 '14 at 15:52
source share

Assuming the syntax file has a good indentation for the java script, visually highlight the block and press =. This works for Java, so I expect it to do something half decent for a java script. The results probably also depend on the settings for tabstop, expandtab, and possibly shiftwidth.

gq is also useful, it formats strings, not padding them.

-one
Mar 08 '09 at 14:38
source share



All Articles