如何正确设置Vim的自动缩进以编辑Python文件?

96

我在设置Vim(7.1.xxx)编辑Python文件(*.py)时遇到了问题。 缩进似乎有问题(最佳为4个空格)。 我已经按照通过Google找到的一些教程操作了,但仍然没有效果 :/ 请帮忙。


2
你的问题是什么?缩进有什么错误? - cschol
1
你使用的是哪个平台?Windows/Mac/Linux? - Jamie
4
请将以下内容添加至 .vimrc 文件中:filetype plugin indent on。此命令的作用是启用文件类型检测、插件和自动缩进功能。 - Aven Desta
7个回答

85

我在我的 MacBook 上使用这个:

" configure expanding of tabs for various file types
au BufRead,BufNewFile *.py set expandtab
au BufRead,BufNewFile *.c set expandtab
au BufRead,BufNewFile *.h set expandtab
au BufRead,BufNewFile Makefile* set noexpandtab

" --------------------------------------------------------------------------------
" configure editor with tabs and nice stuff...
" --------------------------------------------------------------------------------
set expandtab           " enter spaces when tab is pressed
set textwidth=120       " break lines when line length increases
set tabstop=4           " use 4 spaces to represent tab
set softtabstop=4
set shiftwidth=4        " number of spaces to use for auto indent
set autoindent          " copy indent from current line when starting a new line

" make backspaces more powerfull
set backspace=indent,eol,start

set ruler               " show line and column number
syntax on               " syntax highlighting
set showcmd             " show (partial) command in status line

(编辑,仅显示与缩进/制表符相关的内容)


5
在编辑 C 风格的编程语言时,不要使用制表符,将“noexpandtab”替换为“expandtab”。 - badeip
1
@AlexKreimer 你可能是对的 - 我写这个是在2008年 - 那真是很久以前了。我很想更新它,但我已经不再使用vim来做大部分的事情了。当你找到更好的解决方案时,请务必回到这里发布一个链接或自己编写一个更好的答案! - Daren Thomas
@DarenThomas 在我看来,这是一个非常过时的回答。 - Alex Kreimer

15

一个更简单的选项:只需取消注释 /etc/vim/vimrc 文件中以下部分的注释(该部分原本是被注释掉的):

    if has("autocmd")
      filetype plugin indent on
    endif

15

我使用:

$ cat ~/.vimrc
syntax on
set showmatch
set ts=4
set sts=4
set sw=4
set autoindent
set smartindent
set smarttab
set expandtab
set number

但是我将尝试Daren的条目


2
请注意,smartindent仅适用于编辑C文件,而不适用于Python文件(而且现在已经被弃用了,请参见https://dev59.com/d3VC5IYBdhLWcg3woCnN#234578)。 - corwin.amber

5

3

确保您正在编辑VIM的正确配置文件。特别是如果您使用的是Windows,那么该文件可能被命名为_vimrc而不是像其他平台上的.vimrc。

在vim中输入

:help vimrc

并使用以下命令检查_vimrc/.vimrc文件的路径:

:echo $HOME

:echo $VIM

确保您只使用一个文件。如果您想将配置拆分为较小的块,可以从_vimrc文件内部源其他文件。

:help source


1

结合Daren和Thanos提出的解决方案,我们有一个好的.vimrc文件。

-----
" configure expanding of tabs for various file types
au BufRead,BufNewFile *.py set expandtab
au BufRead,BufNewFile *.c set noexpandtab
au BufRead,BufNewFile *.h set noexpandtab
au BufRead,BufNewFile Makefile* set noexpandtab

" --------------------------------------------------------------------------------
" configure editor with tabs and nice stuff...
" --------------------------------------------------------------------------------
set expandtab           " enter spaces when tab is pressed
set textwidth=120       " break lines when line length increases
set tabstop=4           " use 4 spaces to represent tab
set softtabstop=4
set shiftwidth=4        " number of spaces to use for auto indent
set autoindent          " copy indent from current line when starting a new line
set smartindent
set smarttab
set expandtab
set number

" make backspaces more powerfull
set backspace=indent,eol,start

set ruler                           " show line and column number
syntax on               " syntax highlighting
set showcmd             " show (partial) command in status line


0

如果您需要更高级的Python编辑功能,请考虑安装simplefold Vim插件。它允许您使用正则表达式进行高级代码折叠。我使用它来折叠我的类和方法定义,以便更快地进行编辑。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接