在MacVim中为python编写脚本时,如何设置移位宽度以缩进两个空格?

时间:2015-07-01 18:21:36

标签: vim macvim

这是我的〜/ .vimrc文件。我的缩进适用于vim,但不适用于MacVim。每次我打开MacVim并尝试在python中编写脚本时,我都要点击编辑 - >文件设置 - > Shiftwidth - > 2

我需要对〜/ .vimrc文件进行哪些更正?我甚至考虑制作〜/ .vimrc文件的精确副本并将其命名为〜/ .mvimrc。

  1 set nocompatible
  2 
  3 execute pathogen#infect() 
  4 syntax on
  5 filetype plugin indent on
  6 set noai      ai
  7 set nosi      si
  8 set nosta     sta
  9 set shiftwidth=2
 10 set softtabstop=2
 11 set ts=2
 12 set expandtab
 13 set number 
 14 
 15 set statusline+=%#warningmsg#
 16 set statusline+=%{SyntasticStatuslineFlag()}
 17 set statusline+=%*
 18 
 19 let g:syntastic_always_populate_loc_list = 1
 20 let g:syntastic_auto_loc_list = 1
 21 let g:syntastic_check_on_open = 1
 22 let g:syntastic_check_on_wq = 0
 23  
"~/.vimrc" [readonly] 31L, 589C

2 个答案:

答案 0 :(得分:2)

在过去,Python ftplugin只处理“通用”内容,但是半新近更新添加了缩进设置以强制执行PEP8:

" As suggested by PEP8.
setlocal expandtab shiftwidth=4 softtabstop=4 tabstop=8

clean 覆盖这些设置的方法是将以下行添加到~/.vim/after/ftplugin/python.vim

setlocal shiftwidth=2
setlocal softtabstop=2

关于vimrc的一些评论:

set nocompatible    generally useless, remove this line
set noai      ai    needlessly complex and ambiguous, use set autoindent
set nosi      si    smartindent is useless, remove this line
set nosta     sta   needlessly complex and ambiguous, use set smarttab
set ts=2            useless, 'tabstop' should be left at its default value

答案 1 :(得分:0)

你的插件和其他设置是否在macvim中加载,或者是否在完全检测到.vimrc时遇到问题?如果它完全找不到,您可能需要检查this question并查看是否可以修复它。如果它只发生在python上,你有可能有一个改变shiftwidth的插件吗?如果是这样,您可能需要考虑使用后执行此操作。 如果你的vimrc正在加载,你可能想尝试这样做,只为python文件设置shiftwidth为2 ...

您可以使用自动命令检测您正在编辑.py文件类型并更改shiftwidth。

autocmd Filetype py setl shiftwidth=2

在该行之上,你可能需要把它放在:

filetype on

将这两个放在vimrc中以使其正常工作。

非常自我解释,但只是说明它的作用,当它检测到.py文件时,它将移位宽度设置为2.Setl是setlocal(缓冲区)的简写。