DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Python IDE based on vim


Buffer Switch

CtrlP: C-p, C-n

Command Palette

Vim-CtrlP-CmdPalette: -

File Tree

Nerdtree: TAB

Undo history

undotree

Toggle Maximize pane

itmammoth/maximize.vim

Interactive linter/fixer in editor

flake8 + yapf + ALE

First install linter flake8 and fixer yapf:

conda config --add channels conda-forge
conda search flake8
conda search yapf
conda acitvate <my-env>
conda install flake8
conda install yapf

Then integrate these tools with w0rp/ale by adding the following lines into $MYVIMRC:

let g:ale_enabled = 1
let b:ale_linters = {'python': ['flake8']}
let b:ale_fixers = {'python': ['yapf']}

Plug 'w0rp/ale'

Or disable ALE by default through setting g:ale_enabled to 0.

Auto completion & Goto-Definition

jedi + deoplete-jedi + jedi-vim

For example, to analyze Mario-Level-1:

First install jedi-vim and deoplete-jedi or neovim, then run:

git clone https://github.com/justinmeister/Mario-Level-1.git
cd Mario-Level-1
python -m venv envs
. activate envs/bin/activate
pip install pygame ipython pynvim jedi
vi data/main.py

Now move the cursor to c.TIMEOUT and press <leader>d (which is ,d for my nvim), it will jump to TIMEOUT in file data/constants.py.

If it doesn't work, try to modify g:python3_host_prog in .vimrc to the python in the virtualenv. Or try 'deoplete-plugins/deoplete-jedi'

Type checker

Type check is only for Python 3.5+ and optional. Install mypy with conda install -c anaconda-platform mypy.

See dsnote Typing System in Python 3 for details.

Code snippet management

See dsnote Vim Snippet Plugins for details.

Smart code completion and other tools

'python-mode/python-mode' provides auto-completion specific for Python, and check the code style with PEP8 and syntax with PyFlakes (via pylama), such as unassigned variables, incomplete statements, etc.

However it use rope to complete words, while rope doesn't support Python 3 by now. So I choose supertab to replace rope. super-tab search words in all the project files, which is very practical and smart.

Add the following lines into ~/.vimrc and install with Vundle:

let g:pymode_lint_on_fly = 0
let g:pymode_breakpoint = 1
let g:pymode_breakpoint_bind = '<leader>b'
let g:pymode_folding = 0
cabbrev af PymodeLintAuto<CR>

Plugin 'python-mode/python-mode'
Plugin 'ervandew/supertab'

Now you can:

  • Auto lint and syntax check after saving file;

  • Auto format codes according to PEP8 with :af;

  • Run current buffer with <leader>r;

  • Show doc of the current word with K;

See :h pymode-features for details.

Note:

  • python-mode now (2018.1.17) uses git submodules to organize codes, which Vundle can handle automatically for you.

  • python-mode seems conflict with jedi-vim according to jedi-vim doc. So remove jedi-vim after installing python-mode.

Ignore Specific Lint Errors and Warnings

The problem is describe in "PEP8 – import not at top of file with sys.path" in SO (stackoverflow.com).

Option 1: ignore in your editor:

Add let g:pymode_lint_ignore = ["E501", "W",] into ~/.vimrc to ignore E501 and all warnings when linting the Python source files. Search ignore in :h pymode for more details.

Option 2: ignore for specific line:

Add # nopep8 or # noqa at the end of the line where raise the error.

Ref: "How to disable a pep8 error in a specific file?" in SO.

Go to Definitions

I want to get tags for whole project without opening all files one by one. But xolox/vim-easytags and majutsushi/tagbar can only create tags for the opening file. So I choose to build the tag file manually with ctags.

First install ctags with sudo apt install exuberant-ctags.

The default tag file name is tags, I prefer the name .tags. So in $MYVIMRC, add set tags=.tags;. Notice the semicolon at the end, it will make vim search for .tags in parent folder of each level until it find one. Without it, you can only use tags when the working directory of the editor is the project root.

In the root folder of the Python project, run ctags -R -f .tags .. Add .tags in file .gitignore.

Now you can use Ctrl-] to go to function definition, and Ctrl-o to jump back.

Note: you can config ctags in file ~/.ctags:

--python-kinds=-iv
--exclude=build
--exclude=dist

according to ‘Go to Definition’ in Vim for Python using Ctags, Done Right.

Find and Replace

Using plugin brooth/far.vim, find the word under cursor with :F Ctrl-r Ctrl-w **/*.py. Here Ctrl-r Ctrl-w is the key shortcut for getting the word under cursor in command line mode.

Note:

In command line mode (start with pressing : in normal mode), using Ctrl-r % to get the current file name; Ctrl-r / to get the last search pattern, etc. See details of key shortcuts in command line mode with :h c_CTRL-R.


Origin post in 2017:

命令执行

前提:vim有 python 标志:vim --version|ag python

安装 vim-cellmode 插件,在.vimrc中加入:

" vimux cellmode
let g:cellmode_tmux_panenumber='2'
let g:cellmode_default_mappings='0'
vnoremap <silent> <leader>v :call RunTmuxPythonChunk()<CR>
nnoremap <silent> <leader>l :call RunTmuxPythonLine()<CR>
noremap <silent> <leader>nc :call RunTmuxPythonCell(0)<CR>
noremap <silent> <leader>c :call RunTmuxPythonCell(1)<CR>
noremap <silent> <leader>ut :call RunTmuxPythonAllCellsAbove()<CR>

Plugin 'benmills/vimux'
Plugin 'julienr/vim-cellmode'

关于vimscript的语法,例如function! MyFunc() range 中, ! 的含义(覆盖同名函数),range 关键字的含义(函数自己处理范围), 参考 :h :function

自动补全

前提: * 安装jedi:pip install --user jedi * vim有 conceal 标志:vim --version|ag conceal

安装 jedi-vim 插件: 在.vimrc中加入Plugin 'davidhalter/jedi-vim'



Published

Sep 14, 2017

Last Updated

Jan 6, 2020

Category

Tech

Tags

  • ide 26
  • python 136
  • vim 92

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor