PETR KOUT - ENGINEERING AND PHYSICS

MY PERSONAL SITE AND BLOG ABOUT SOFTWARE AND HARDWARE ENGINEERING, AND PHYSICS

High Productivity Vim Configuration for Python Development

By Petr Kout, August 26, 2016


High Productivity Vim Configuration for Python Development

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):

Download .vimrc file

Download .gvimrc file

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+oMoves the cursor to the previous position
Ctrl+iMoves the cursor to the next position
HMoves the cursor to the top of the page
LMoves the cursor to the bottom of the page
MMoves the cursor to the middle of the page
zzScrolls the page such that the cursor is vertically in the middle of the page
ztScrolls the page such that the cursor is at the top of the page
zbScrolls the page such that the cursor is at the bottom of the page
Ctrl+eScrolls one line down
Ctrl+yScrolls one line up
f+characterPuts the cursor before the first occurrence of the character ahead
:messOpens the messages buffer
giSwitches 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+lSwitches to the right pane
Ctrl+hSwitches to the left pane
Ctrl+jSwitches to the pane above
Ctrl+kSwitches 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+vCreates a vertical split (or :vsp)
:spCreates a horizontal split
:10spCreates a horizontal split of height equal to 10 lines
Ctrl+w+rSwap left and right or top and bottom splits
Ctrl+x, Ctrl+lCompletes a whole line
yyYanks text
pPastes text after the cursor
PPastes text before the cursor
"ayyYanks text into the register "a"
"aPPastes text from the register "a" before the cursor
"0PPastes from the register "0"; the "yank" register; uneffected by the "dd" delete command
"1PPastes from the register "1"; the "delete" register; uneffected by the "yank" command
:regShows the contents of registers
*Finds occurrences of the word under the cursor forwards
#Finds occurrences of the word under the cursor backwards
VuLowercases a line
VUUppercases a line
veuLowercases a word
veUUppercases a word
gfOpens 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.

ShortcutDescription
Leader+fPuts Grep snippet into the command line for me to fill with desired custom values
Leader+pPrints the path to the of the active buffer

CTRL-P

CTRLP

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.

ShortcutDescription
Ctrl+p Brings up CTRL-P window
Ctrl+dToggles search by file name vs by directory name only
[F5]Purges the cache
Ctrl+n, Ctrl+pOpens browse history
Ctrl+yCreates a new file
Ctrl+zSelects multiple files
Ctrl+oOpens the selected file(s)
Ctrl+h, Ctrl+lMoves cursor within the typed text
Ctrl+fMoves forwards in selection
Ctrl+bMoves backwards in selection
[TAB]Completes current file/directory typed name
Ctrl+cCloses the CTRL-P window
Ctrl+aMoves to the beginning of the prompt
Ctrl+eMoves to the end of the prompt
Ctrl+uClears the prompt
Ctrl+tOpens the selected file in a new tab
Ctrl+sOpens the selected file in a horizontal split
Ctrl+vOpens the selected file in a vertical split
[F7]Clears the most recently used (MRU) list
Ctr+\Inserts from various sources
file.txt:45Opens the file file.txt and puts cursor to line 45
file.txt:/thisOpens 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.

ShortcutDescription
Leader+ntToggle NerdTree window
Leader+nfSelect 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!

ShortcutDescription
Leader+numberSwitch 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.

ShortcutDescription
Leader+Leader+wHighlights the characters to jump to to the right of the cursor
Leader+Leader+bHighlights the characters to jump to to the left of the cursor
Leader+Leader+f+oHighlights all the "o" characters to the right of the cursor
Leader+Leader+F+oHighlights all the "o" characters to the left of the cursor
Leader+Leader+t+oHighlights all the characters appearing before "o"'s to the right of the cursor
Leader+Leader+T+oHighlights all the characters appearing before "o"'s to the left of the cursor
Leader+Leader+b+oHighlights the characters at the beginning of each line below
Leader+Leader+b+kHighlights the characters at the beginning of each line above
Leader+Leader+s+oSearch 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.

ShortcutDescription
:RestartVimRestarts Vim and restores the current session
:OpenSessionOpens a session from a session list (I map it to Leader+o+s)
:CloseSessionCloses the current session
:DeleteSessionDeletes 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.

ShortcutDescription
Ctrl+nIterates through omnicompletion options
Ctrl+[space]Lists autocompletion options
Ctrl+dGo to definition
KShow pydoc documentation for the item currently under the cursor
Leader+rRename all occurrences of the variable under the cursor
Leader+nPopulates 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.

ShortcutDescription
]]Jumps to the next class or function
[[Jumps to the previous class or function
]mJumps to the next class or method
[mJumps to the previous class or method
vaCSelects the class under the cursor
daCDeletes the class under the cursor
yaCYanks the class under the cursor
vaMSelects the function/method under the cursor
daMDeletes the function/method under the cursor
yaMYanks the function/method under the cursor
:PymodeLintChecks the current buffer for errors
Ctrl+c, rrRenames the function/method/class/variable under the cursor
Ctrl+c, r1rRenames the current module
Ctrl+c, roOrganizes Python module imports
Ctrl+c, raInserts import for the current word under the cursor
Ctrl+c, rmExtract method from from the selected lines
Ctrl+c, rlExtract variable from the selected lines
Ctrl+c, rsChange 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.

ShortcutDescription
Ctrl+nPressing repeatedly selects occurrences of the word under the cursor
vWhen selection is complete, go to the Visual mode
sWhen selection is complete, go to the Insert mode and delete the selected text
Shift+iWhen selection is complete, go to the Insert mode and move cursor to the beginning of the selected word
Ctrl+pUnselect the last selected word
Ctrl+xSkip the last selected word and go to the next word
:MultipleCursorsFind argSelects 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.

ShortcutDescription
F8Toggles Tagbar on and off
F9Opens Tagbar and puts the cursor on the current method/class/variable
sToggles between sorting by name and order
[space]Displays the method/function prototype in the status bar
pJumps to the tag under the cursor, but does not close the Tagbar window
POpens the tag under the cursor in a preview window
Ctrl+nGoes to the next fold
Ctrl+pGoes to the previous fold
+Unfolds the fold under the cursor
=Folds all the folds in the window
-Folds the folder under the cursor
z+ROpens all folds up to level 99
zMCloses all folds up to level 0
cToggles the autoclose option
xToggles the zooming window option
qCloses 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.

ShortcutDescription
:bdDeletes the current buffer and closes its window
:bwWipes the current buffer and closes its window
:BDSame as :bd, but leaves the window open
:BWSame as :bw, but leaves the window open
:BBMoves backward through the list of recently open buffers
:BFMoves forward through the list of recently open buffers
:BAMoves 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.

ShortcutDescription
:Ctrl+eOnce a snippet is selected from autocompletion options, it expands the snippet

Useful Links

http://rayninfo.co.uk/vimtips.html – A great page listing many advanced power user Vim commands.

http://vimawesome.com – Great resources for Vim users