描述一下您为Python/Django开发定制的Vim编辑器?

2024-10-01 02:23:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近在Python/Django开发中完全转向了Vim。我花了很多时间来定制它,直到今天,上帝知道我很难找到适合Python/Django开发的最佳vim插件的帮助。

我决定问这个问题,这样像我这样的人就能直接从你的经历中受益: 您已经构建了完美的Python/Djangoish Vim编辑器?为我们描述它(插件,脚本,customized.vimrc,colorschemes等等)。

谢谢

我的配置

好的,这是我自己的配置。实际上,我已经选择创建一个简单的Vim配置,这样我就可以掌握我选择安装的少量插件,而不是创建一大堆我永远都不会掌握或使用的插件。这是我使用最多的插件列表:

  • 对于文件管理
  • SnipMate这是TextMate的snippets特性的实现
  • 代码完成由Omnicompletion处理,Omnicompletion在Vim中是默认的
  • Pydoc要将Python文档集成到Vim中
  • TagList对于源代码浏览,在大型文件中非常有用。
  • Pyflakes脚本以动态突出显示Python代码并发出警告

另外,我在$HOME/.vim/ftplugin/中创建了一个python.vim文件,其中包含这个脚本,因此我可以通过运行Shift+e从vim运行python代码:

" Execute file being edited with <Shift> + e:
map <buffer> <S-e> :w<CR>:!/usr/bin/env python % <CR>

我还收集了一些有用的.vimrc定制:

set nocompatible    " use vim defaults
set number          " show line numbers
colorscheme desert
set tags=tags;$HOME/.vim/tags/ "recursively searches directory for 'tags' file
set expandtab       " tabs are converted to spac
set tabstop=4       " numbers of spaces of tab character
set shiftwidth=4    " numbers of spaces to (auto)indent
set showcmd         " display incomplete commands
set hlsearch        " highlight searches
set incsearch       " do incremental searching
set ruler           " show the cursor position all the time
set numberwidth=4   " line numbering takes up 5 spaces
set ignorecase      " ignore case when searching
set nowrap          " stop lines from wrapping
filetype plugin indent on " turn on the indent plugins
syntax on                 " syntax highlighing
" TagList Plugin Configuration
let Tlist_Ctags_Cmd='/usr/bin/ctags'       " point taglist to ctags
let Tlist_GainFocus_On_ToggleOpen = 1      " Focus on the taglist when its toggled
let Tlist_Close_On_Select = 1              " Close when something's selected
let Tlist_Use_Right_Window = 1             " Project uses the left window
let Tlist_File_Fold_Auto_Close = 1         " Close folds for inactive files
" Omnicompletion functions
autocmd FileType python set omnifunc=pythoncomplete#Complete
autocmd FileType javascript set omnifunc=javascriptcomplete#CompleteJS
autocmd FileType html set omnifunc=htmlcomplete#CompleteTags
autocmd FileType css set omnifunc=csscomplete#CompleteCSS
au FileType py set expandtab
au FileType py set foldmethod=indent
map <F2> :previous<CR>                  " map F2 to open previous buffer
map <F3> :next<CR>                      " map F3 to open next buffer
map <F4> :NERDTreeToggle<CR>            " map F4 to open NERDTree
map <F5> :TlistToggle<CR>               " map F5 to toggle the Tag Listing
map <silent><C-Left> <C-T>              " taglist - map Ctrl-LeftArrow to jump to the method/property under your cursor
map <silent><C-Right> <C-]>             " taglist - map Ctrl-RhitArrow to jump back to your source code
map <silent><A-Right> :tabnext<CR>      " map Alt-RightArrow to jump to the next tab
map <silent><A-Left> :tabprevious<CR>   " map Alt-LeftArrow to jump to the previous tab

Tags: theto插件mapontagsvimcr
2条回答

我不打算在这里发布我的整个.vimrc文件,但我有一个与您类似的设置。不过,这不是特定于Python/Django的,除了snipMate和Python模式的一些自定义片段。这里是我正在使用的vim插件:

  • Pathogen:vim dir中组织得更好的vim插件结构
  • comments.vim:使用ctrl-c和ctrl-x进行更快的特定语言注释
  • NERDTree
  • NERDTree tabs
  • syntastic:语法检查插件(对我来说主要用于非python代码)
  • surround.vimautoclose.vim:更容易处理括号、打开和关闭标记等
  • matchit:扩展了%命令,以匹配并循环通过例如html标记。对于遍历Python代码语句(例如,如果elif else),可以下载python_match.vim并将其放入ftplugin/python/目录中。我把它放进~/.vim/bundle/matchit/ftplugin/python/
  • python-mode:伟大的Python编辑插件。在文件保存时自动执行pyflakes/pep8检查(如果需要,也可以使用pylint)。不过,我在我的.vimrc文件中通过let g:pymode_rope = 0禁用了自动完成,因为每次保存文件时它都会滞后于我。还扩展了python代码的语法突出显示。
  • snipMate(下面是python的自定义代码片段)
  • tagBar:我不能没有一个巨大代码文件的大纲。

一些我经常使用的自定义python片段:

snippet #utf
    # -*- coding: utf-8 -*-

snippet ds
    """
    ${1: }
    """
# just the first (or last) three quites for the docstring
snippet dss
    """${1: }
# For file headers
snippet dsfile  
    """
    :File: ${1:`Filename('$1.py', 'foo.py')`}
    :Author: ${2:`g:snips_author`}
    :Description: ${3}
    """
snippet pdb
    import pdb
    pdb.set_trace()

我并没有太多特定于Django的mod,尽管我给了jinja2语法比Django模板语法更高的优先级。

特别是对于Python:

  • 对于Python语法检查,我使用PyFlakes和highlight SpellBad ctermbg=darkred
  • 有时(很少)我觉得需要自动完成,在这种情况下,我使用Eclim
  • 剩下的,默认的东西。标签大小4,软标签等。。。

Vim设置:

  • 256色方案desert256

    if ((&term == 'screen') || (&term == 'screen-bce') || (&term == 'xterm')) 
        set t_Co=256                                                          
        set t_Sb=^[[4%dm                                                      
        set t_Sf=^[[3%dm                                                      
        colo desert256                                                        
    endif
    
  • 很多标签(tabetabn
  • 大量拆分(垂直和水平)

相关问题 更多 >