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

ygya80vv  于 2022-12-13  发布在  Python
关注(0)|答案(7)|浏览(178)

我在设置Vim(7.1.xxx)编辑Python文件(*.py)时遇到了麻烦。缩进似乎被破坏了(最佳4个空格)。我已经按照谷歌上找到的一些教程学习了。仍然没有效果:/请帮助。

guykilcj

guykilcj1#

我在我的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

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

jhkqcmku

jhkqcmku2#

我用途:

$ 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

但我还是要试试达伦的作品

fnvucqvd

fnvucqvd3#

更简单的选项:只需取消注解/etc/vim/vimrc文件中配置的以下部分(最初已注解掉):

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

uqxowvwt5#

确保为VIM编辑正确的配置文件。特别是在使用Windows时,文件可以命名为_vimrc,而不是其他平台上的.vimrc。
体内类型
:help vimrc
并使用以下命令检查_vimrc/.vimrc文件的路径
:echo $HOME
:echo $VIM
确保您只使用一个文件。如果您想将配置拆分成更小的块,您可以从_vimrc文件中获取其他文件。
:help source

yhived7q

yhived7q6#

结合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
dxxyhpgq

dxxyhpgq7#

对于更高级的python编辑,请考虑安装simplefold vim插件。它允许您使用正则表达式进行高级代码折叠。我用它来折叠我的类和方法定义,以便更快地编辑。

相关问题