Skip to content

Instantly share code, notes, and snippets.

@wlonk
Last active December 30, 2018 23:27
Show Gist options
  • Save wlonk/4fc811057c7c8ad31dcf3cb8985dede8 to your computer and use it in GitHub Desktop.
Save wlonk/4fc811057c7c8ad31dcf3cb8985dede8 to your computer and use it in GitHub Desktop.
Test python-mode motions
" First, we don't care about vi compatability.
set nocompatible
" Need to turn filetype off when starting Vundle
filetype off
" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
Plugin 'gmarik/Vundle.vim'
Plugin 'python-mode/python-mode' " Make sure this version has the patch applied!
call vundle#end()
" Turn filetype back on now that Vundle is done
filetype plugin indent on
syntax on
set number
set ruler

Guided tour:

Using the attached .vimrc, open sample.py.

First, class/def motions. With the cursor at row 1, column 1, ]] should move your cursor to row 15, col 1. ]] again will move you to row 19, column 1. [[ will move you back to row 15, column 1. Reset to row 1, column 1 with gg. Repeat with ]C and [C and it should behave the same as with ]] and [[, which I find surprising, but it preserves existing behavior so I will keep it.

Now def motions. Reset to row 1, column 1, via gg. ]M should move you to row 2, column 1. ]M again should move you to row 5, column 1. [M should reverse this, moving you to row 2, column 1.

Now text objects. We will use visual selection to show text objects, but not change the file. Move the cursor to row 6, column 1. viM should select inside the current method (i.e. just selecting row 6). Escape out of that selection. vaM should select around the current method, that is, rows 5, 6 and 7.

Without this patch, any method prefixed with async will be skipped by these motions and text objects.

To play around:

Open this in vim, with the patch I supplied, and use some combination of:

]] to move to the next top-level class or def (async or not), [[ to move to the previous.

]C to move to the next top-level class (or def, which is weird behavior, but preserving pre-existing weirdness), [C to move to the previous.

]M to move to the next def, async or not, [M to move to the previous.

And for text objects, any of v, c, d, etc and then iC to get the inside of a class, aC to get all around a class, iM to get the inside of a def (sync or async), and aM to get the outside of a def (sync or async). I tend to use v as it's least destructive, and clearly shows what the plugin identifies as "in" or "around".

class Foo:
def bar1(self):
pass
async def baz1(self):
pass
def bar2(self):
pass
async def baz2(self):
pass
async def foo1():
pass
def foo2():
pass
async def foo3():
pass
def foo4():
pass
class Bar:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment