Turning Vim into a modern Python IDE
来源:互联网 发布:为什么淘宝登陆不上去 编辑:程序博客网 时间:2024/04/28 12:03
Contents
- Intro
- Basic Editing and Debugging
- Code Folding
- Window Splits
- Snippets
- Task lists
- Revision History
- Syntax Highlighting and Validation
- Pep8
- Tab Completion and Documentation
- Code Navigation
- Buffers
- Fuzzy Text File Search
- File Browser
- Refactoring and Go to definition
- Searching
- Integration with Git
- Test Integration
- django nose
- py.test
- Virtualenv
- Django
- Random Tips
TL;DR:
$ git clone https://github.com/sontek/dotfiles.git$ cd dotfiles$ ./install.sh vim
Download PDF Version
Intro
Back in 2008, I wrote the article Python with a modular IDE (Vim). Years later, I have people e-mailing me and commenting daily asking for more information, even though most of the information in it is outdated. Here is the modern way to work with Python and Vim to achieve the perfect environment.
Because one of the most important parts about a development environment is the ability to easily reproduce across machines, we are going to store our vim configuration in git:
$ mkdir ~/.vim/$ mkdir ~/.vim/{autoload,bundle}$ cd ~/.vim/$ git init
The purpose of the autoload directory is to automatically load the vim pluginPathogen, which we'll then use to load all other plugins that are located in the bundle directory. So download pathogen and put it in your autoload folder.
You'll need to add the following to your ~/.vimrc so that pathogen will be loaded properly. Filetype detection must be off when you run the commands so its best to execute them first:
filetype offcall pathogen#runtime_append_all_bundles()call pathogen#helptags()
Now lets add all of the vim plugins we plan on using as submodules to our git repository:
git submodule add http://github.com/tpope/vim-fugitive.git bundle/fugitivegit submodule add https://github.com/msanders/snipmate.vim.git bundle/snipmategit submodule add https://github.com/tpope/vim-surround.git bundle/surroundgit submodule add https://github.com/tpope/vim-git.git bundle/gitgit submodule add https://github.com/ervandew/supertab.git bundle/supertabgit submodule add https://github.com/sontek/minibufexpl.vim.git bundle/minibufexplgit submodule add https://github.com/wincent/Command-T.git bundle/command-tgit submodule add https://github.com/mitechie/pyflakes-pathogen.gitgit submodule add https://github.com/mileszs/ack.vim.git bundle/ackgit submodule add https://github.com/sjl/gundo.vim.git bundle/gundogit submodule add https://github.com/fs111/pydoc.vim.git bundle/pydocgit submodule add https://github.com/vim-scripts/pep8.git bundle/pep8git submodule add https://github.com/alfredodeza/pytest.vim.git bundle/py.testgit submodule add https://github.com/reinh/vim-makegreen bundle/makegreengit submodule add https://github.com/vim-scripts/TaskList.vim.git bundle/tasklistgit submodule add https://github.com/vim-scripts/The-NERD-tree.git bundle/nerdtreegit submodule add https://github.com/sontek/rope-vim.git bundle/ropevimgit submodule initgit submodule updategit submodule foreach git submodule initgit submodule foreach git submodule update
Thats it! Now that we've got our vim configuration in git!
Now lets look at how to use each of these plugins to improve the power of vim:
Basic Editing and Debugging
Code Folding
Lets first enable code folding. This makes it a lot easier to organize your code and hide portions that you aren't interested in working on. This is quite easy for Python, since whitespace is required.
In your ~/.vimrc just add:
set foldmethod=indentset foldlevel=99
Then you will be able to be inside a method and type 'za' to open and close a fold.
Window Splits
Sometimes code folding isn't enough; you may need to start opening up multiple windows and working on multiple files at once or different locations within the same file. To do this in vim, you can use these shortcuts:
Vertical Split : Ctrl+w + vHorizontal Split: Ctrl+w + sClose current windows: Ctrl+w + q
I also like to bind Ctrl+<movement> keys to move around the windows, instead of using Ctrl+w + <movement>:
map <c-j> <c-w>jmap <c-k> <c-w>kmap <c-l> <c-w>lmap <c-h> <c-w>h
Snippets
The next tweak that really speeds up development is using snipmate. We've already included it in our bundle/ folder so its already enabled. Try opening up a python file and typing 'def<tab>'. It should stub out a method definition for you and allow you to tab through and fill out the arguments, doc string, etc.
I also like to create my own snippets folder to put in some custom snippets:
$ mkdir ~/.vim/snippets$ vim ~/.vim/snippets/python.snippets
Put this in the file:
snippet pdb import pdb; pdb.set_trace()
Now you can type pdb<tab> and it'll insert your breakpoint!
Task lists
Another really useful thing is to mark some of your code as TODO or FIXME! I know we all like to think we write perfect code, but sometimes you just have to settle and leave a note for yourself to come back later. One of the plugins we included was the tasklist plugin that will allow us to search all open buffers for things to fix. Just add a mapping to open it in ~/.vimrc:
map <leader>td <Plug>TaskList
Now you can hit <leader>td to open your task list and hit 'q' to close it. You can also hit enter on the task to jump to the buffer and line that it is placed on.
Revision History
The final basic editing tweak I suggest everyone start utilizing is the Gundo plugin. It'll allow you to view diff's of every save on a file you've made and allow you to quickly revert back and forth:
Just bind a key in your .vimrc to toggle the Gundo window:
map <leader>g :GundoToggle<CR>
Syntax Highlighting and Validation
Simply enable syntax highlighting in your ~/.vimrc:
syntax on " syntax highlighingfiletype on " try to detect filetypesfiletype plugin indent on " enable loading indent file for filetype
Because we enabled pyflakes when we added it as a submodule in ~/.vim/bundle, it will notify you about unused imports and invalid syntax. It will save you a lot of time saving and running just to find out you missed a colon. I like to tell it not use the quickfix window:
let g:pyflakes_use_quickfix = 0
Pep8
The final plugin that really helps validate your code is the pep8 plugin, it'll make sure your code is consistent across all projects. Add a key mapping to your ~/.vimrc and then you'll be able to jump to each of the pep8 violations in the quickfix window:
let g:pep8_map='<leader>8'
Tab Completion and Documentation
Vim has many different code completion options. We are going to use the SuperTab plugin to check the context of the code you are working on and choose the best for the situation. We've already enabled the SuperTab plugin in the bundle/ folder, so we just have to configure it to be context sensitive and to enable omni code completion in your ~/.vimrc:
au FileType python set omnifunc=pythoncomplete#Completelet g:SuperTabDefaultCompletionType = "context"
Now we just enable the menu and pydoc preview to get the most useful information out of the code completion:
set completeopt=menuone,longest,preview
We also enabled the pydoc plugin at the beginning with all the submodules; that gives us the ability to hit <leader>pw when our cursor is on a module and have a new window open with the whole documentation page for it.
Integration with Git
We installed 2 plugins, git.vim and fugitive, that give us all the integration we need. Git.vim will provide us syntax highlighting for git configuration files; fugitive provides a great interface for interacting with git including getting diffs, status updates, committing, and moving files.
Fugitive also allows you to view what branch you are working in directly from vim. Add this to your statusline in ~/.vimrc:
%{fugitive#statusline()}
The big commands you need to know:
- Gblame: This allows you to view a line by line comparison of who the last person to touch that line of code is.
- Gwrite: This will stage your file for commit, basically doing git add <filename>
- Gread: This will basically run a git checkout <filename>
- Gcommit: This will just run git commit. Since its in a vim buffer, you can use keyword completion (Ctrl-N), like test_all<Ctrl-N> to find the method name in your buffer and complete it for the commit message. You can also use + and - on the filenames in the message to stage/unstage them for the commit.
Test Integration
django nose
Test runner integration really depends on the testing library you are using and what type of tests you are running but we included a great generic plugin called MakeGreen that executes off of vim's makeprg variable. So for instance, if you are using django with django-nose you could define a shortcut key in your ~/.vimrc like this:
map <leader>dt :set makeprg=python\ manage.py\ test\|:call MakeGreen()<CR>
This will just give you a green bar at the bottom of vim if your test passed or a red bar with the message of the failed test if it doesn't. Very simple.
py.test
I also included the py.test vim plugin for those who prefer it. This plugin has a lot more functionality including executing individual tests by class, file, or method. You can also cycle through the individual assertion errors. I have the following bindings:
" Execute the testsnmap <silent><Leader>tf <Esc>:Pytest file<CR>nmap <silent><Leader>tc <Esc>:Pytest class<CR>nmap <silent><Leader>tm <Esc>:Pytest method<CR>" cycle through test errorsnmap <silent><Leader>tn <Esc>:Pytest next<CR>nmap <silent><Leader>tp <Esc>:Pytest previous<CR>nmap <silent><Leader>te <Esc>:Pytest error<CR>
Virtualenv
Vim doesn't realize that you are in a virtualenv so it wont give you code completion for libraries only installed there. Add the following script to your ~/.vimrc to fix it:
" Add the virtualenv's site-packages to vim pathpy << EOFimport os.pathimport sysimport vimif 'VIRTUAL_ENV' in os.environ: project_base_dir = os.environ['VIRTUAL_ENV'] sys.path.insert(0, project_base_dir) activate_this = os.path.join(project_base_dir, 'bin/activate_this.py') execfile(activate_this, dict(__file__=activate_this))EOF
Django
The only true django tweak I make is before I open vim I'll export the DJANGO_SETTINGS_MODULE environment so that I get code completion for django modules as well:
export DJANGO_SETTINGS_MODULE=project.settings
Random Tips
If you want to find a new color scheme just go tohttp://code.google.com/p/vimcolorschemetest/ to preview a large selection.
© John Anderson <sontek@gmail.com> 2011
- Turning Vim into a modern Python IDE
- Turning vim as a modern python IDE
- Turning Vim into a modern Python IDE(打造Python Vim 编辑器)
- Turning Your iPhone Into a Remote Control
- How to turn Vim into a full-fledged IDE
- Python -- Make vim as a python IDE
- change your vim into IDE
- Turning an HTTP Proxy Server into a Wireless Internet Gateway
- vim as a IDE
- VIM as Python IDE
- vim 打造python IDE
- vim---打造Python IDE
- vim as python IDE
- vim & Python ide
- From Program to Product: Turning Your Code into a Saleable Product
- Mobile Device Hacking: Turning your phone, PDA or internet tablet into a pen-testing platform
- linux vim python ide 配置
- vim打造成PYTHON IDE
- UNIX网络编程--实现并发UDP服务器
- 详细介绍c++/vc++标准模板库(STL)中的map的使用攻略!
- Qt-QTableWidget
- AndroidManifest.xml文件解析
- 最大堆和最小堆找中位数
- Turning Vim into a modern Python IDE
- cocos2d-x学习日志(8) --瓦片地图集
- IBM讲解服务器推送技术
- snmp学习笔记——开篇
- BMP算法程序
- java的LINQ :Linq4j简明介绍
- linux内核链表详解
- Groovy学习
- Python图表绘制:matplotlib绘图库入门