简体   繁体   English

.vimrc配置为Python

[英].vimrc configuration for Python

My current .vimrc configuration is below: 我目前的.vimrc配置如下:

set nohlsearch
set ai
set bg=dark
set showmatch
highlight SpecialKey ctermfg=DarkGray
set listchars=tab:>-,trail:~
set list
autocmd BufRead *.py set smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class
set tabstop=4
set shiftwidth=4
set expandtab
set autoindent
set smartindent
syntax on
set listchars=tab:>-
set listchars+=trail:.
set ignorecase
set smartcase
map <C-t><up> :tabr<cr>
map <C-t><down> :tabl<cr>
map <C-t><left> :tabp<cr>
map <C-t><right> :tabn<cr>

However, when I write python scripts, when I push "ENTER", it will go to the BEGINNING of the next line. 但是,当我编写python脚本时,当我按下“ENTER”时,它将转到下一行的BEGINNING。 What do I add so that it will auto-tab for me? 我添加了什么以便它会自动为我标记?

Try this: 试试这个:

filetype indent on
filetype on
filetype plugin on

I primarily do Python programming and this is the brunt of my vimrc 我主要做Python编程,这是我的vimrc的首要任务

set nobackup
set nowritebackup
set noswapfile
set lines=40
set columns=80
set tabstop=4
set shiftwidth=4
set softtabstop=4
set autoindent
set smarttab
filetype indent on
filetype on
filetype plugin on

The short answer is that your autocmd is missing the BufEnter trigger, so it isn't being fired when you create a new file. 简短的回答是您的autocmd缺少BufEnter触发器,因此在创建新文件时不会触发它。 Try this instead: 试试这个:

 au BufEnter,BufRead *.py setlocal smartindent cinwords=if,elif,else,for,while,try,except,finally,def,class

Note that I also changed the set to setlocal . 请注意,我还将set更改为setlocal This'll prevent these options from stomping on your other buffers' options. 这将阻止这些选项踩踏你的其他缓冲区选项。

The "right" way to do what you're trying to do is to add filetype indent on to your .vimrc. 执行您要执行的操作的“正确”方法是将filetype indent on添加到.vimrc。 This'll turn on the built-in filetype based indentation. 这将打开基于内置文件类型的缩进。 Vim comes with Python indentation support. Vim附带了Python缩进支持。 See :help filetype-indent-on for more info. 有关详细信息,请参阅:help filetype-indent-on

Consider having a look at the official .vimrc for following PEP 7 & 8 conventions. 考虑查看官方.vimrc以了解遵循PEP 7和8约定。 Present over here 出现在这里

http://svn.python.org/projects/python/trunk/Misc/Vim/vimrc http://svn.python.org/projects/python/trunk/Misc/Vim/vimrc

You shouldn't have to explicitly indent python keywords. 您不必显式缩进python关键字。 The $VIM/indent/python.vim file takes care of that. $ VIM / indent / python.vim文件负责处理。 You just need to turn on filetype indent and autoindent. 你只需要打开filetype indent和autoindent。

I (simply) use this: 我(简单地)使用这个:

set shiftwidth=4
set tabstop=4
set expandtab
filetype plugin on
filetype indent on
syntax on

I'd say that this configuration provides something, without causing conflicts ( /etc/vim/vimrc ): 我会说这个配置提供了一些东西,而不会引起冲突( /etc/vim/vimrc ):

" Python Setup
autocmd Filetype python setlocal ts=2 sw=2 expandtab
autocmd Filetype python set number
autocmd Filetype python set autoindent
autocmd Filetype python set expandtab
autocmd Filetype python set shiftwidth=4
autocmd Filetype python set cursorline
autocmd Filetype python set showmatch
autocmd Filetype python let python_highlight_all = 1
autocmd Filetype python set colorcolumn=80

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM