High Productivity Vim Configuration for Python Development

vim

I’ve been using Vim for scripting and software development for many years now. Yes, the initial learning curve was steep, but with a bit of rigor and discipline, you can become comfortable with the tool in a matter of a couple of weeks. Once you get the basics wired into your brain’s neural networks, there is no way back – you’re hooked and every other editor for coding feels clumsy and inefficient. Over the years, I collected a bunch of very useful plugins that I use for my daily work. I wrote this post to share the most fundamental keyboard shortcuts and plugins that I have added to my toolkit over the years. This Vim setup is focused on people who code in Python and/or do web development, but many of these tips are language agnostic. Let’s get going!

The .vimrc File

I use the following .vimrc and .gvimrc files (you only need .vimrc if you don’t use GVim):

http://petrkout.com/download/vimrc
http://petrkout.com/download/gvimrc

Important Note! Notice that the file downloaded files’ names don’t start with . (period). If you end up using those files directly instead of copy/pasting their content, don’t forget to rename them to .vimrc and .gvimrc before placing them in your home directory.

These contain the configuration necessary for the shortcuts and plugins appearing in this writeup. Install the files (or piggyback from them) and install the plugins. I use Vundle an Plug as Vim plugin managers. They make installing the plugins is easy. If you don’t have those installed yet, go to

https://github.com/VundleVim/Vundle.vim

and

https://github.com/junegunn/vim-plug

and follow the installation instructions first.

Then, to install the plugins specified in the .vimrc file, start Vim and enter:

:PluginInstall
:PlugInstall

When you’re done with it, all the following should apply to your Vim:

General Vim

These are some basic shortcuts that go beyond the super basics, such as h, j, k, l keys to move around.

Shortcut Description
Ctrl+o Moves the cursor to the previous position
Ctrl+i Moves the cursor to the next position
H Moves the cursor to the top of the page
L Moves the cursor to the bottom of the page
M Moves the cursor to the middle of the page
zz Scrolls the page such that the cursor is vertically in the middle of the page
zt Scrolls the page such that the cursor is at the top of the page
zb Scrolls the page such that the cursor is at the bottom of the page
Ctrl+e Scrolls one line down
Ctrl+y Scrolls one line up
f+character Puts the cursor before the first occurrence of the character ahead
:mess Opens the messages buffer
gi Switches to the INSERT mode and put the cursor where it was left before previously exiting that mode
di( Deletes everything within the parenthesis
dt( Deletes everything preceding the parenthesis
df( Deletes everything preceding the parenthesis and the parenthesis itself
Ctrl+l Switches to the right pane
Ctrl+h Switches to the left pane
Ctrl+j Switches to the pane above
Ctrl+k Switches to the pane below
Ctrl+w+| Maximizes the width of the current pane
Ctrl+w+= Sets panes to equal size
Ctrl+w+_ Maximizes the height of the current pane
Ctrl+w+v Creates a vertical split (or :vsp)
:sp Creates a horizontal split
:10sp Creates a horizontal split of height equal to 10 lines
Ctrl+w+r Swap left and right or top and bottom splits
Ctrl+x, Ctrl+l Completes a whole line
yy Yanks text
p Pastes text after the cursor
P Pastes text before the cursor
\"ayy Yanks text into the register "a" (skip the \ character – this plugin forces me to put it there)
\"aP Pastes text from the register "a" before the cursor (skip the \ character – this plugin forces me to put it there)
\"0P Pastes from the register "0"; the "yank" register; uneffected by the "dd" delete command; skip the \ character – this plugin forces me to put it there
\"1P Pastes from the register "1"; the "delete" register; uneffected by the "yank" command; skip the \ character – this plugin forces me to put it there
:reg Shows the contents of registers
* Finds occurrences of the word under the cursor forwards
# Finds occurrences of the word under the cursor backwards
e+\. Opens a file explorer
Vu Lowercases a line
VU Uppercases a line
veu Lowercases a word
veU Uppercases a word
gf Opens the file name under the cursor
g; Cycles backwards through changes
g, Cycles forwards through changes
q: Opens the command line history window

Custom Vim Key Mappings

The following are some key mappings that I customized myself for my own comfort. I use the “,” (comma) character as my Leader character because it’s very easily accesssible and doesn’t conflict with almost any other shortcuts.

Shortcut Description
Leader+f Puts Grep snippet into the command line for me to fill with desired custom values
Leader+p Prints the path to the of the active buffer

CTRL-P

Ctrl-P

CTRL-P is a great plugin that will replicate the famous Sublime Text’s CTRL+P shortcut to quickly find and open a desired file within your project file system. The following are some useful key combinations that will significantly boost your file finding.

Shortcut Description
Ctrl+p Brings up CTRL-P window
Ctrl+d Toggles search by file name vs by directory name only
[F5] Purges the cache
Ctrl+n, Ctrl+p Opens browse history
Ctrl+y Creates a new file
Ctrl+z Selects multiple files
Ctrl+o Opens the selected file(s)
Ctrl+h, Ctrl+l Moves cursor within the typed text
Ctrl+f Moves forwards in selection
Ctrl+b Moves backwards in selection
[TAB] Completes current file/directory typed name
Ctrl+c Closes the CTRL-P window
Ctrl+a Moves to the beginning of the prompt
Ctrl+e Moves to the end of the prompt
Ctrl+u Clears the prompt
Ctrl+t Opens the selected file in a new tab
Ctrl+s Opens the selected file in a horizontal split
Ctrl+v Opens the selected file in a vertical split
[F7] Clears the most recently used (MRU) list
Ctr+\ Inserts from various sources
file.txt:45 Opens the file file.txt and puts cursor to line 45
file.txt:/this Opens the file file.txt and puts cursor to the first occurrence of the word "this"

NerdTree

NERDTree

NERDTree is a useful plugin when you are looking for a file whose name you don’t quite remember and need to find it by searching through the project folder hierarchy. The NERDTree will equip your Vim with an easy-to-toggle window that will give you a hierarchical, browsable file and folder view similar to that found in Sublime Text or big IDEs. It shouldn’t be used as the go-to tool to open file because there are easier ways to do that in Vim, but it’s useful for a quick hierarchical overview ft the project file system layout.

Shortcut Description
Leader+nt Toggle NerdTree window
Leader+nf Select the currently open file in the open NerdTree window
Leader+no  Open the NerdTree window in the current working directory

Airline

airline

Airline is a plugin that will give you, besides many other useful features, access to your open buffers via a tab at the top of your Vim window. It can be configured to easily switch to any of your open buffers via a shortcut using the little superscript numbers inside each tab label representing one open buffer. Highly recommended!

Shortcut Description
Leader+number Switch to the tab labeled with the given number

EasyMotion

EasyMotion

This one is a bit tricky to get under your sleeve, but once you wire it in, your ability to move quickly within an open buffer increases multiplefold. Meet EasyMotion. This plugin lets you instantaneously jump from your current cursor location to anywhere within the visible screen with just a few key strokes rather than holding your navigational key (h, j, l, or k) down and waiting until your cursor works its way there.

Shortcut Description
Leader+Leader+w Highlights the characters to jump to to the right of the cursor
Leader+Leader+b Highlights the characters to jump to to the left of the cursor
Leader+Leader+f+o Highlights all the "o" characters to the right of the cursor
Leader+Leader+F+o Highlights all the "o" characters to the left of the cursor
Leader+Leader+t+o Highlights all the characters appearing before "o"'s to the right of the cursor
Leader+Leader+T+o Highlights all the characters appearing before "o"'s to the left of the cursor
Leader+Leader+b+o Highlights the characters at the beginning of each line below
Leader+Leader+b+k Highlights the characters at the beginning of each line above
Leader+Leader+s+o Search the "o" character in both directions

Session

Session

Meet Session, the plugin that will allow you to save your current Vim session and restore it later to its current state. Why is this useful? It is the Vim’s way of saving a project and restoring it later with the added benefit that it not only remembers what folder to set as your current directory, it also restores the buffers that you had open in your tabs the last time and restores all your custom Vim settings as well.

Shortcut Description
:RestartVim Restarts Vim and restores the current session
:OpenSession Opens a session from a session list (I map it to Leader+o+s)
:CloseSession Closes the current session
:DeleteSession Deletes the current session

Jedi-Vim

jedi-vim

JEDI-Vim is an extremely versatile and powerful plugin that equips Vim with features that are better known in the big IDEs, such as autocompletion, go-to-definition, rename all occurrences of a variable, and so on.

Shortcut Description
Ctrl+n Iterates through omnicompletion options
Ctrl+[space] Lists autocompletion options
Ctrl+d Go to definition
K Show pydoc documentation for the item currently under the cursor
Leader+r Rename all occurrences of the variable under the cursor
Leader+n Populates the quickwindow with all files that point to the definition under the cursor

Python-Mode

Python-mode

If you do Python development in Vim, this plugin is a “must have”. It provides a large set of time saving features that you usually find in the big IDEs, but specifically optimized for Python. These include jumping to a class or function definition, selecting the entire function in one swoop, refactoring, auto-inserting imports, and so on. The list is long. It’s worth reading through the plugin’s documentation.

Shortcut Description
]] Jumps to the next class or function
[[ Jumps to the previous class or function
]m Jumps to the next class or method
[m Jumps to the previous class or method
vaC Selects the class under the cursor
daC Deletes the class under the cursor
yaC Yanks the class under the cursor
vaM Selects the function/method under the cursor
daM Deletes the function/method under the cursor
yaM Yanks the function/method under the cursor
:PymodeLint Checks the current buffer for errors
Ctrl+c, rr Renames the function/method/class/variable under the cursor
Ctrl+c, r1r Renames the current module
Ctrl+c, ro Organizes Python module imports
Ctrl+c, ra Inserts import for the current word under the cursor
Ctrl+c, rm Extract method from from the selected lines
Ctrl+c, rl Extract variable from the selected lines
Ctrl+c, rs Change function signature

Multiple Cursors

Multiple cursors

This plugins is very useful for refactoring. Whenever you need to edit multiple occurrences of a text on the screen, you can either do a regex find and replace operation, or you can use multiple cursors to easily select all needed occurrences of your text and then edit them all simultaneously.

Shortcut Description
Ctrl+n Pressing repeatedly selects occurrences of the word under the cursor
v When selection is complete, go to the Visual mode
s When selection is complete, go to the Insert mode and delete the selected text
Shift+i when selection is complete, go to the Insert mode and move cursor to the beginning of the selected word
Ctrl+p Unselect the last selected word
Ctrl+x Skip the last selected word and go to the next word
:MultipleCursorsFind arg Selects all words that match the regular expression argument `arg`

Tagbar

Tagbar

Tagbar is very useful for a quick navigation through the variables and functions within the currently open buffer. A simple shortcut brings up the Tagbar window and lets you easily navigate to the desired identifier. Once there, press Enter and the cursor will jump to the respective location in the open buffer.

Shortcut Description
F8 Toggles Tagbar on and off
F9 Opens Tagbar and puts the cursor on the current method/class/variable
s Toggles between sorting by name and order
[space] Displays the method/function prototype in the status bar
p Jumps to the tag under the cursor, but does not close the Tagbar window
P Opens the tag under the cursor in a preview window
Ctrl+n Goes to the next fold
Ctrl+p Goes to the previous fold
+ Unfolds the fold under the cursor
= Folds all the folds in the window
- Folds the folder under the cursor
z+R Opens all folds up to level 99
zM Closes all folds up to level 0
c Toggles the autoclose option
x Toggles the zooming window option
q Closes the Tagbar window

Bufkill

Bufkill

Bufkill provides means to easily delete, wipe or navigate through the open buffers. Without it, you’d have to list the open buffers to know their numbers before closing them or switching between them. Bufkill is a big timesaver.

Shortcut Description
:bd Deletes the current buffer and closes its window
:bw Wipes the current buffer and closes its window
:BD Same as :bd, but leaves the window open
:BW Same as :bw, but leaves the window open
:BB Moves backward through the list of recently open buffers
:BF Moves forward through the list of recently open buffers
:BA Moves to the alternate buffer whilst preserving the cursor column

UltiSnips

Ultisnips

UltiSnips is a large selection of code snippets across many languages. Once you start typing a keyword, such as “class”, a quick-view of  available snippets will appear for you to select from. As soon as you select one, the skeleton of the selected snippet gets inserted into your code ready for you to just tweak it.

Shortcut Description
:Ctrl+e Once a snippet is selected from autocompletion options, it expands the snippet

Useful Links

A solution to the /python2.7/site-packages/_mysql.so: undefined symbol: mysql_kill error

mysqlI use Django with MySQL and I this error bothers me almost every time I start a new project with a fresh new installation of Django and relevant Python packages. I use virtualenv to isolate my Python environment for each project. The problem that’s causing this error is that the Python MySQL libraries that are installed via aptitude (I am on Ubuntu) are too old and need to be recompiled.

Continue reading