In this article 5 Vim shortcuts to speed up your Python development. These techniques are saving me tons of repeated cycles allowing me to better concentrate on the important: coding.
Vim's learning curve might be steep, but with practice you start to 'edit text at the speed of light' which makes you a better developer. This is not an article about what is the best editor, there are other awesome options: Emacs, PyCharm, Sublime ... I just love Vim and use it for almost all my editing.
Before diving in you need to know what a "Leader Key" is:
The "Leader key" is a way of extending the power of VIM's shortcuts by using sequences of keys to perform a command. The default leader key is backslash. Therefore, if you have a map of
Q, you can perform that action by typing \Q. - StackOverflow answer
I mapped mine to comma using this in my vimrc:
let mapleader = ","
So when I provide mappings like ,p ,f ,a and you use another mapleader, replace my comma with your key.
Add this to your vimrc:
nmap ,p :w<CR>:!python3 %<CR>
It first saves the output (:w), then runs (!) the current script (%)
For Python 2 I got another shortcut:
nmap ,2 :w<CR>:!python2.7 %<CR>
I also have ,v to run the Python of my active virtual env:
nmap ,v :w<CR>:!venv/bin/python %<CR>
As featured in our pep8 article I find it very useful to flake8 my code before committing. If you have not heard of flake8, it is "the modular source code checker", a wrapper around PyFlakes, pycodestyle and Ned Batchelder’s McCabe script. You need the vim-flake8 plugin. I invoke it with ,f - having this in my vimrc:
autocmd FileType python map <buffer> ,f :call Flake8()<CR>
You will need the Conque plugin.
To open a Python in vertical split I use the 'cp' shortcut, vimrc:
nmap cp :ConqueTermVSplit python3<CR>
Of course you can specify any script. The advantage is that any generated output becomes editable with Vim.
Some years ago I made a script to query StackOverflow in a split window using Conque. I wanted to refactor that script but recently I discovered a better option: howdoi. You can use this plugin to run it inside Vim. Very cool.
I use Ctrl + Z / fg a lot to go back and forth between coding and version control. You can also type :sh / Ctrl + d.
For testing I usually open script.py and test_script.py alongside each other with:
$ vi -O script.py test_script.py
This is why PEP8's "Limit all lines to a maximum of 79 characters" is important: no wrapping lines.
Then use Ctrl + w + w to toggle between the split windows. If you want to open another file in vertical split you can run this from Vim's Command Mode:
Another way to interact with the command line is via Vim's Command Mode. While writing this article I found the useful q: shortcut.
I use NERD tree which opens a nice file tree you can navigate with regular Vim strokes, I mapped it to Ctrl + x
map <C-x> :NERDTreeToggle<CR>
Similar to the howdoi intergration I managed to call an external script and paste its output into Vim. I used pyperclip to manage the clipboard and switched to MacVim because of clipboard support:
$ /usr/bin/vim --version |grep clipboard -clipboard $ /Applications/MacVim.app/contents/MacOS/Vim --version|grep clipboard +clipboard
See the script here. It takes a previously copied Amazon URL from the clipboard, converts it to an affiliation link and pastes it back to the clipboard.
To run it and paste its output back into Vim I made this mapping in my vimrc:
nmap ,a :!genlink<CR><ESC>"+p # having genlink in PATH and pyperclip installed
"+ is the clipboard buffer.
Vim keys when at the start of previous line:
0 # go to begin line fh # go to h "+d$ # cut URL to clipboard ,a # run the create link script and insert link where cursor is
How cool is that? This saves time and made me think what other repeated tasks I can automate and integrate into Vim :)
If you have ideas or things you built let me know in the comments.
This only scratched the surface. Some great articles on Vim + Python:
Not Vim related, but now that we are talking shortcuts, here are two Python related ones I got in my bashrc:
As soon as I start a new project I run 'pvenv' to create and start a fresh new virtual env:
alias pvenv='python -m venv venv && source venv/bin/activate'
Run all unittests in the current directory:
alias utest='python -m unittest discover'
(although at the time of this writing I discovered the nosetests script)
Keep Calm and Code in Python!
Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.
"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H
"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B
"This is now my favourite first Python go-to reference." - Anthony L
"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S