2011-10-05 98 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