2011-10-05 39 views
3

我知道这已经被问过了,但是我无法让JavaScript缩进在Vim中正常工作。Vim中的JavaScript语法和缩进

我尝试安装这个插件:

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

我得到这个行为:

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

这是我的vimrc:

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

我也有这个试了一下插件:

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

但是,这给了我这样的:

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

即两个选项卡,在这里我只是一个标签中,要缩进。

任何人有什么想法在这里做什么?

回答

4

你有没有在你的.vimrc

set smarttab 
set cindent 

编辑也JavaScript的“插件”我用VIM是javascript.vim它取代默认的VIM JavaScript语法文件尝试这个。

无论您使用什么插件,VIM中的缩进通常都很糟糕,并且是VIM用户的常见抱怨,尤其是JavaScript。没有完美的解决方案,考虑到VIM的强大可扩展性,这很奇怪。

+1

我在Vim的工作了一整天的JavaScript和唐Javascript缩进似乎没有任何实际问题。看看我在Github上的Vim dotfiles - > [https://github.com/shanestillwell/dotvim](https://github.com/shanestillwell/dotvim) –

6

我从谷歌来到这里,对上面建议的伊钊的缩进文件不满意。仍然没有捕捉到我的一些嵌套功能。

我在twitter上问了一遍,并建议https://github.com/pangloss/vim-javascript - 我非常高兴。

HTH,

+0

非常感谢!你是对的......你建议的[pangloss](https://github.com/pangloss/vim-javascript)比[Yi Zhao]更好(http://www.vim.org/scripts/ script.php?script_id = 1491)和[jelera](https://github.com/jelera/vim-javascript-syntax)替代品。 –

1

Vim的维基解释了如何设置特定文件类型压痕,它是相当直接的:http://vim.wikia.com/wiki/Indenting_source_code#Different_settings_for_different_file_types

最简单的方法就是把你的.vimrc文件autocmd FileType说明。您可以为单独的每个文件类型指定缩进:

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 

或设置默认缩进所有文件类型,并覆盖它的特定的:

set tabstop=4 
set shiftwidth=4 

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