Vim as python IDE
Here are some things you can do to make a nice programming environment for Python only using Vim.
Vim and Python
By default, Vim already has some mappings that work with Python:
[[Jump backwards to begin of current/previous toplevel[]Jump backwards to end of previous toplevel][Jump forwards to end of current toplevel]]Jump forwards to begin of next toplevel[mJump backwards to begin of current/previous method/scope[MJump backwards to end of previous method/scope]MJump forwards to end of current/next method/scope]mJump forwards to begin of next method/scope
gd jumps to the place where a function is declared ("goto definition").
Flake8 Linter
I run a linter via the following plugin: Plug 'nvie/vim-flake8'.
The plugin only takes care of the integration with Vim, so do make sure you actually install flake8 on your system.
I run the linter every time a Python buffer is saved.
augroup python
autocmd BufWritePost *.py call Flake8() " Run linter on saving
"Other stuff ...
augroup END
And I did some extra customization:
" Flake8 linting
let g:flake8_show_in_gutter=1
let g:flake8_show_in_file=1
" Customize markers
let g:flake8_error_marker='x>'
let g:flake8_warning_marker='~>'
" Do not show quickfix (bit too intrusive for my taste)
let g:flake8_show_quickfix=0
" Unset markers ('flake unset' aka 'fuck u')
nnoremap <leader>fu :call flake8#Flake8UnplaceMarkers()<CR>
" Show error message of current line in the rules
" 'flake current'
nnoremap <leader>fc :call flake8#Flake8ShowError()<CR>
" Use colors defined in the colorscheme
highlight link Flake8_Error Error
highlight link Flake8_Warning WarningMsg
highlight link Flake8_Complexity WarningMsg
highlight link Flake8_Naming WarningMsg
highlight link Flake8_PyFlake WarningMsg
Deoplete + Jedi
Deoplete is a modern completion engine.
It is mainly designed for NeoVim so sometimes functionality for Vim 8 is a bit more limited.
Nevertheless, the Jedi plugin for Deoplete works great for me.
" Deoplete + Jedi
Plug 'deoplete-plugins/deoplete-jedi'
Plug 'davidhalter/jedi-vim'
I mainly rely on it for jumping to function definitions in other files.
By default, Vim already has some functionality for this gd (go to definition), but Deoplete supercharges this.
It for example allows you to jump to function definitions even if it's defined in another file (of your own, or of a Python package); or to list all usages of a particular function (important for refactoring!).
" Jedi-vim (deoplete + python)
let g:jedi#popup_on_dot = 0
let g:jedi#popup_select_first = 1
let g:jedi#goto_command = "<leader>g"
let g:jedi#goto_assignments_command = "<leader>a"
let g:jedi#goto_stubs_command = "<leader>s"
let g:jedi#goto_definitions_command = ""
let g:jedi#documentation_command = "K"
let g:jedi#usages_command = "<leader>u"
let g:jedi#completions_command = "<C-Space>"
let g:jedi#rename_command = "<leader>r"
CtrlP
I use CtrlP for my fuzzy finding. Within a programming project this quickly helps me locate files.
N.B.
A lot of people use the amazing fzf.
The only reason I currently don't is that CtrlP is a Vimscript implementation that is fully cross-platform, whereas I always have issues with fzf on Windows.
There are some additional fuzzy find functions you may leverage when programming.
For example, with CtrlPBufTag you can fuzzy search definition names and jump to the definitions.
Integration with Vim terminal
On Linux, I use window managers such as i3wm and dwm, where typically Vim runs in one window and my terminal running Python in another.
However, in a Linux environment integration between Vim and the terminal is much smoother than on Windows.
One good option is to run Vim and Python in two different tmux windows and have bindings for sending keys back and forth.
The following plugin is useful for that:
Plug 'christoomey/vim-tmux-navigator'
On Windows with gVim I don't have such a terminal-based workflow and I've always struggled to find the perfect setup.
The default terminal is the limited and outdated cmd.exe.
You can use powershell, but I've never gotten it to work to my satisfaction.
If can't recall exactly what issues I had, but at least it requires changing all Vim shell settings and I think it also created problems with some plugins.
But from Vim 8 on, Vim has an integrated terminal :term so I decided to use that.
Because this terminal lives inside a Vim window, many things that require some effort become trivial, such as copying outputs back into Vim, navigating between the editor and terminal windows, or automatically sending keys back and forth.
The main problem I had with this setup is that loading my conda environments does not work in this terminal so I did not have access to my packages.
Conda relies on startup scripts and integrates with a selected amount of shells (of which Powershell is one).
Perhaps using Powershell properly from within Vim could solve it, but I didn't bother.
Also, the Python used within the Vim terminal is the one Vim is compiled with and not the installation I use in Powershell, so this makes managing packages even more annoying.
Long story short, I decided to just use venv and this works perfectly!
A really nice feature of IDEs and of Jupyter Notebooks is that they allow you to run things interactively, which is super useful for data exploration and manipulation. What I want is to run iPython in a terminal window and then interactively send lines of code to that sessions, like you would do in an IDE.
Because we are now fully inside a Vim environment, we can use default Vim functionality for this! I created two bindings to do two things: 1) Send the current line to the interactive Python session 2) Send a visual selection of lines to the interactive Python session
nnoremap <leader>i :y \| :call term_sendkeys(bufnr(&shell), @")<CR>
vnoremap <leader>v :y \| :call term_sendkeys(bufnr(&shell), @")<CR>
You'll see these commands are identical, except that the second command can be called on visual selections (it's a visual remap).
How does this work?
- We first simply copy our current line or our the current/latest visual selection
- We then want to send the copied keys stored under register
"(retrieved with@") to the Vim buffer in which our interactive session runs - For that, we need to know the buffer number of our terminal session. If we know the name of the session, we can find the corresponding buffer number with the
bufnr()command.&shellis the Vim variable that stores the shell name (:echo(&shell)). In my case this returnscmd.exe, which is also the default name given to the buffer. - We send the copied content
@"to the buffer with buffer numberbufnr(&shell)and enter it.
NOTE: I don't think using the default Vim shell variable always works. You can make a variable with the correct buffer name and use that instead:
let g:shellname = 'cmd.exe'
nnoremap <leader>i :y \| :call term_sendkeys(bufnr(shellname), @")<CR>
vnoremap <leader>i :y \| :call term_sendkeys(bufnr(shellname), @")<CR>
Another nifty detail is that when pasting in multiple lines, iPython automatically indents new lines.
If you copy in a tab/spaces (which you will when pasting in functions), you'll end up with an extra tab per line.
So for this to work it is necessary to call %autoindent when starting your interactive Python session.
(It should be possible to make this a global setting and avoid this step, but I didn't do this).
This is the workflow that now works for me:
- Set up your environment using
venv - Open a Vim terminal with
:term - Activate your environment within that Vim terminal with the
venvactivate script in theenvfolder. - Start
iPython - Toggle autoindent off with
%autoindent - Use our bindings to send and execute code interactively in our Vim terminal session.