2

I'm using vim version 8.0 on OS X (installed through Homebrew) and it keeps indenting object literals wrong. The first key and only the first key is flush with the previous line. All subsequent keys are indented four spaces.

var object = {
    key1: "value1",
    key2: "value2"
};

as

var object = {
key1: "value1",
    key2: "value2",
};

The javascript indentation file is here.

/usr/local/share/vim/vim80/indent/javascript.vim

It is pretty clearly the pangloss/vim-javascript plugin.

cat /usr/local/share/vim/vim80/indent/javascript.vim  | head -n 5
" Vim indent file
" Language: Javascript
" Maintainer: Chris Paul ( https://github.com/bounceme )
" URL: https://github.com/pangloss/vim-javascript
" Last Change: August 25, 2016

STR:

% touch awesome.js
% vim -u NONE awesome.js

:set filetype=javascript

type out javascript example

enter image description here

gg=G

voilà incorrectly indented code.

enter image description here

If there isn't some hidden configuration setting that's causing the bundled indenter for javascript to act up, is there a way to completely shadow <...>/indent/javascript.vim and <...>/syntax/javascript.vim so I can start writing a replacement?

5
  • 1
    You will need to use :filetype indent on before :set ft=javascript to enable loading the indentation file. So your minimal test is a bit too minimal here :-) If I do this, it works as expected for me. Try adding :filetype plugin indent on to your vimrc. Does that fix it? Dec 17, 2016 at 1:05
  • @Carpetsmoker my real vimrc already has filetype plugin indent on in it, but I will update the test and post new and better screenshots if anything changed. Dec 17, 2016 at 1:16
  • Uh oh... Looks like it's back to the drawing board for figuring out what's up with my full vimrc. If I follow your suggestion I get the right indentation behavior. Dec 17, 2016 at 1:19
  • 1
    Try this. It's probably just some setting or plugin. Dec 17, 2016 at 1:28
  • 1
    @Carpetsmoker I figured it out. I had set nomagic and it looks like it was interfering with the parser. Dec 17, 2016 at 1:50

1 Answer 1

1

As of 2016-12-16, the javascript.vim indenter will break if you set nomagic.

The current workaround is to not use set nomagic.

3
  • 1
    Note that you can expect lots of stuff to break when you change the magic setting. This is documented in :help 'magic' :-) "WARNING: Switching this option off most likely breaks plugins! That is because many patterns assume it's on and will fail when it's off." Dec 17, 2016 at 2:26
  • @Carpetsmoker I thought there was some way to write regular expressions with an explicit magic level to prevent exactly this kind of thing from happening. Dec 17, 2016 at 2:30
  • 1
    Yes, you can with \m. But almost no one does that. Dec 17, 2016 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.