我在设置Vim(7.1.xxx)编辑Python文件(*.py)时遇到了问题。 缩进似乎有问题(最佳为4个空格)。 我已经按照通过Google找到的一些教程操作了,但仍然没有效果 :/ 请帮忙。
我在设置Vim(7.1.xxx)编辑Python文件(*.py)时遇到了问题。 缩进似乎有问题(最佳为4个空格)。 我已经按照通过Google找到的一些教程操作了,但仍然没有效果 :/ 请帮忙。
我在我的 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
(编辑,仅显示与缩进/制表符相关的内容)
一个更简单的选项:只需取消注释 /etc/vim/vimrc 文件中以下部分的注释(该部分原本是被注释掉的):
if has("autocmd")
filetype plugin indent on
endif
我使用:
$ 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的条目
smartindent
仅适用于编辑C文件,而不适用于Python文件(而且现在已经被弃用了,请参见https://dev59.com/d3VC5IYBdhLWcg3woCnN#234578)。 - corwin.amberset softtabstop=4
确保您正在编辑VIM的正确配置文件。特别是如果您使用的是Windows,那么该文件可能被命名为_vimrc而不是像其他平台上的.vimrc。
在vim中输入
:help vimrc
并使用以下命令检查_vimrc/.vimrc文件的路径:
:echo $HOME
:echo $VIM
确保您只使用一个文件。如果您想将配置拆分为较小的块,可以从_vimrc文件内部源其他文件。
:help source
结合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
filetype plugin indent on
。此命令的作用是启用文件类型检测、插件和自动缩进功能。 - Aven Desta