简体   繁体   English

vim-在.vimrc中设置时python autoindent无法正常工作

[英]vim - python autoindent not working correctly when set in .vimrc

I am having issues with my .vimrc file: I am correctly writing python code. 我的.vimrc文件有问题:我正确地编写python代码。 But I have indentation issue. 但是我有缩进问题。 When I put is in my .vimrc file: 当我放在.vimrc文件中时:

" indentation python
au BufNewFile,BufRead *.py
    \ set tabstop=4
    \ set softtabstop=4
    \ set shiftwidth=4
    \ set textwidth=79
    \ set expandtab
    \ set ts=4
    \ set sw=4
    \ set ai
    \ set autoindent
    \ set fileformat=unix
    \ set expandtab ts=4 sw=4 ai

But as soon as a python file is open, if I run set expandtab ts=4 sw=4 ai vim will start-indent correctly when select code and hit S-= . 但是,一旦打开python文件,如果我运行set expandtab ts=4 sw=4 ai vim,将在选择代码并按S-=时正确地开始缩进。 Any idea? 任何想法?

This is not a valid syntax in Vim. 这在Vim中不是有效的语法。 \\ only cancels the newline, it does not let you chain commands (you have | for that). \\仅取消换行符,它不允许您链接命令(为此具有| )。 What you wrote is equivalent to (truncated to just a subset of your options): 您编写的内容等同于(截断为选项的一部分):

au BufNewFile,BufRead *.py set tabstop=4 set softtabstop=4 set shiftwidth=4 set textwidth=79

which obviously doesn't make sense, since set is not a valid option. 这显然没有意义,因为set不是一个有效的选项。

So you can either do 所以你可以做

au BufNewFile,BufRead *.py
  \ set tabstop=4
  \     softtabstop=4
  \     shiftwidth=4
  \     textwidth=79

(make it a single set command with multiple settings), or you can do (使其成为具有多个设置的单个set命令),或者您可以执行

au BufNewFile,BufRead *.py
    \ set tabstop=4 |
    \ set softtabstop=4 |
    \ set shiftwidth=4 |
    \ set textwidth=79

(make it multiple set commands). (使其成为多个set命令)。

However, this is bad practice, since the options will be set globally; 但是,这是一个坏习惯,因为这些选项将在全局范围内设置。 eg if you load a JavaScript file then a Python file, then go back to your JavaScript buffer it will have your Python settings. 例如,如果您加载一个JavaScript文件,然后加载一个Python文件,然后返回您的JavaScript缓冲区,它将具有您的Python设置。 You should prefer to use setlocal instead. 您应该更喜欢使用setlocal

Also, it generally makes sense to use FileType python instead of BufNewFile,BufRead *.py - maybe not specifically in Python's case, but some languages can have multiple extensions. 另外,通常使用FileType python代替BufNewFile,BufRead *.py是有意义的-在Python的情况下可能不专门,但是某些语言可以具有多种扩展名。

And finally, this allows you to clean up your .vimrc by placing your language-dependent settings in .vim/after/ftplugin/python.py . 最后,这允许您通过将与语言相关的设置放在.vim/after/ftplugin/python.py来清理.vimrc You don't need autocmd there - just write your setlocal , and be secure in the knowledge that your settings will be executed for each buffer of python filetype. 您在那里不需要autocmd只需编写setlocal ,并确保您的设置将针对每个python文件类型的缓冲区执行,所以请放心。

Thus, my final recommendation: 因此,我的最终建议是:

" .vim/after/ftplugin/python.py
setlocal tabstop=4
setlocal softtabstop=4
setlocal shiftwidth=4
setlocal textwidth=79
setlocal expandtab
setlocal autoindent
setlocal fileformat=unix

or equivalently 或同等

" .vim/after/ftplugin/python.py
setlocal ts=4 sts=4 sw=4 tw=79 et ai ff=unix

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM