我的.vimrc
文件存在问题:
我正在编写python代码。但我有缩进问题。
当我把它放在我的.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
但是一旦python文件打开,如果我运行set expandtab ts=4 sw=4 ai
当选择代码并点击S-=
时,vim将正确缩进。
有什么想法吗?
答案 0 :(得分:3)
这不是Vim中的有效语法。 \
仅取消换行符,它不允许您链接命令(您有|
)。你写的内容相当于(截断到你的选项的一个子集):
au BufNewFile,BufRead *.py set tabstop=4 set softtabstop=4 set shiftwidth=4 set textwidth=79
这显然没有意义,因为set
不是有效选项。
所以你可以做到
au BufNewFile,BufRead *.py
\ set tabstop=4
\ softtabstop=4
\ shiftwidth=4
\ textwidth=79
(使用多个设置制作单个set
命令),或者您可以
au BufNewFile,BufRead *.py
\ set tabstop=4 |
\ set softtabstop=4 |
\ set shiftwidth=4 |
\ set textwidth=79
(使其成为多个set
命令)。
然而,这是不好的做法,因为选项将全局设置;例如如果您加载一个JavaScript文件然后加载一个Python文件,然后返回到您的JavaScript缓冲区,它将具有您的Python设置。您应该更喜欢使用setlocal
。
此外,使用FileType python
代替BufNewFile,BufRead *.py
通常也是有意义的 - 可能不是Python的具体情况,但某些语言可以有多个扩展名。
最后,通过将您的语言相关设置放在.vimrc
中,您可以清理.vim/after/ftplugin/python.py
。您不需要autocmd
- 只需编写setlocal
,并确保您知道将为python
文件类型的每个缓冲区执行您的设置。
因此,我的最终建议是:
" .vim/after/ftplugin/python.py
setlocal tabstop=4
setlocal softtabstop=4
setlocal shiftwidth=4
setlocal textwidth=79
setlocal expandtab
setlocal autoindent
setlocal fileformat=unix
或等效
" .vim/after/ftplugin/python.py
setlocal ts=4 sts=4 sw=4 tw=79 et ai ff=unix