12

I need a motion that moves to end/beginning of a python block, and I need another motion that moves to begining/end of previous/next method/class (all combinations).

I've tried vim-indent-object, python-mode, chapa, vim-python-object, etc. None of them support end of block, and none of them support all combinations of above motions.

I remember once upon a time I had a plugin that provided ]], [[, ][ and [] motions for begining/end of previous/next method/class, but I can't find it in my plugged directory (searched "][" and "[]" in my plugged directory and analyzed all occurrences but didn't find anything that defines "][" or "[]").

Do you know the plugin I used to use? Do you know any plugin that provides this functionality?

  • 3
  • 3
    [[ and ]], are provided by the default python ftplugin. – romainl Apr 8 '16 at 11:16
  • 1
    @romainl Thanks for info, I know about [[, ]], (, ), { and }. [[ and ]] are useful in some cases but my main problem is I need [] and ][ or something like that, which provides jumps to end of next/previous method/class. – Sassan Apr 8 '16 at 13:46
  • 1
    @muru thanks, its ]e solves most of my problems, however some mappings in this plugin seems to be broken like ]v or ]> I'll try to fix them in a fork and publish it. Though I still wonder what was the plugin that provided ][ and [] it was really useful. – Sassan Apr 8 '16 at 13:48
9

With the recent update (Nov 2017) of $VIMRUNTIME/ftplugin/python.vim following ft-specific motions are now available for the python language

  • [[ 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
  • [m Jump backwards to begin of current/previous method/scope
  • [M Jump backwards to end of previous method/scope
  • ]M Jump forwards to end of current/next method/scope
  • ]m Jump forwards to begin of next method/scope

Example python file

class previous:             # [[[[

    def method_first:
        pass
    # ...

    def method_last:
        pass                # []

class current:              # [[ or [m...[m[m

    def method_first:       # [[]m or [m...[m
        pass                # [[]M or [M...[M
    # ...

    def method_previous:    # [m[m
        pass                # [M

    def method_current:     # [m
        # CURSOR <---------------------------
        pass                # ]M

    def method_next:        # ]m
        pass                # ]M]M
    # ...

    def method_last:        # ][[m or ]m...]m
        pass                # ][   or ]M...]M

class previous:             # ]]   or ]m...]m]m

    def method_first:
        pass
    # ...

def global_func:            # ]]]]
    pass

This update should be included if your vim patch level is at least 8.0.1256.

If you can't update but want to use this functionality, you can install the most recent version into the ~/.vim directory:

$ curl https://raw.githubusercontent.com/vim/vim/master/runtime/ftplugin/python.vim --create-dirs -o ~/.vim/ftplugin/python.vim

Plugin Pythonsense

This plugin provides similar motions but slightly modified:

The stock Vim 8.0 "class" motions ("]]", "[[", etc.), find blocks that begin at the first column, regardless of whether or not these are class or function blocks, while its method/function motions ("[m", "]m", etc.) find all blocks at any indent regardless of whether or not these are class or function blocks. In contrast, "Pythonsense" class motions work on finding all and only class definitions, regardless of their indent level, while its method/function motions work on finding all and only method/function definitions, regardless of their indent level.

All details and examples are given at https://github.com/jeetsukumaran/vim-pythonsense#stock-vim-vs-pythonsense-motions.

  • I should wait till neovim brings this patch. – Sassan Nov 7 '17 at 6:18
  • 1
    I guess for the time being you could install it manually curl https://raw.githubusercontent.com/vim/vim/master/runtime/ftplugin/python.vim --create-dirs -o ~/.config/nvim/ftplugin/python.vim. – Hotschke Nov 7 '17 at 6:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.