[英]Vim indent JavaScript files with spaces instead of tab
I have the following vimrc configuration which is working fine for Python files: 我有以下vimrc配置,它对Python文件运行良好:
execute pathogen#infect()
syntax on
filetype plugin indent on
set tabstop=4
So when I press tab it's an alias to 4 spaces. 因此,当我按Tab时,它是4个空格的别名。
But it doesnt seem to work with JavaScript files. 但它似乎不适用于JavaScript文件。
When I press tab it always add a tab plus 2 spaces for every indentation. 当我按Tab时,它总是为每个缩进添加一个Tab加2个空格。 I am ok with the two spaces but how can I replace that tab with 4 spaces?
我可以使用两个空格,但是如何用4个空格替换该选项卡?
All the code is indented with spaces I dont want start adding tabs/spaces mix 所有代码都缩进了空格,我不想开始添加制表符/空格混合
I know you can specify the tabs/spaces for specific files but I cant make it work 我知道您可以为特定文件指定选项卡/空格,但是我无法使其工作
autocmd FileType javascript setlocal shiftwidth=1 tabstop=4
To configure 4-space indentation, you need to :setlocal tabstop=4 expandtab
. 要配置4空格缩进,您需要
:setlocal tabstop=4 expandtab
。 To be safe, it's recommended to also reset the 'softtabstop'
option to 0
. 为了安全起见,建议也将
'softtabstop'
选项重置为0
。 Usually, you want the 'shiftwidth'
option aligned to the chosen indent; 通常,您希望
'shiftwidth'
选项与所选缩进对齐; that would be shiftwidth=4
: 那将是
shiftwidth=4
:
autocmd FileType javascript setlocal shiftwidth=4 tabstop=4 softtabstop=0 expandtab
I would recommend putting the options into ~/.vim/after/ftplugin/javascript.vim
instead of defining lots of :autocmd FileType javascript
; 我建议将选项放入
~/.vim/after/ftplugin/javascript.vim
而不要定义很多:autocmd FileType javascript
; it's cleaner and scales better; 它更干净,缩放性更好; requires that you have
:filetype plugin on
, though. 要求您在上具有
:filetype plugin on
。
If that doesn't work, some filetype plugin (or another :autocmd
) may override your settings. 如果那不起作用,则某些文件类型插件 (或其他
:autocmd
)可能会覆盖您的设置。 You can check with 您可以检查
:verbose setlocal shiftwidth? tabstop? softtabstop? expandtab?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.