开发者

JavaScript syntax & indentation in Vim

I know this has been asked before, but I'm having trouble getting JavaScript indentation to work p开发者_开发技巧roperly in Vim.

I tried installing this plugin:

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

And I get this behaviour:

if (x == 1) {
alert("nice");
}

This is my vimrc:

syntax on
set background=light
colorscheme solarized
set tabstop=4
filetype plugin indent on
let g:solarized_termcolors=16

I also tried it with this plugin:

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

But that gives me this:

if (x == 1) {
        alert("nice");
}

i.e., two tabs, where I only want it to indent by a single tab.

Anyone have any ideas what to do here?


Vim wiki explains how to setup filetype-specific indentation, and it's pretty straight-forward: http://vim.wikia.com/wiki/Indenting_source_code#Different_settings_for_different_file_types

The simplest way is to put autocmd FileType instructions in your .vimrc file. You can specify indentation for each file type separately:

autocmd FileType javascript setlocal shiftwidth=2 tabstop=2
autocmd FileType html       setlocal shiftwidth=2 tabstop=2
autocmd FileType python     setlocal shiftwidth=4 softtabstop=4 expandtab

or set default indentation for all file types, and override it for the specific ones:

set tabstop=4
set shiftwidth=4

autocmd FileType javascript setlocal shiftwidth=2 tabstop=2                                                   
autocmd FileType html setlocal shiftwidth=2 tabstop=2


I came here from google and was unsatisfied with Yi Zhao's indent file as suggested above. Still wasn't catching some nested functions of mine.

I asked around on twitter and was suggested https://github.com/pangloss/vim-javascript - with which I am far happier.

HTH,


Have you tried this in your .vimrc

set smarttab
set cindent

edit also the JavaScript "plugin" I use for VIM is javascript.vim which replaces the default VIM javascript syntax file.

No matter what plugins you use, indenting in VIM is usually pretty bad, and is a common complaint with VIM users, especially with JavaScript. There is no perfect solution, which is strange considering the powerful extensibility of VIM.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜