More efficient movements editing python files in vim

20,867

Solution 1

Square Bracket Mappings [[, ]], [m, ]m and similar

$VIMRUNTIME/ftplugin/python.vim now (2018) remaps all builtin mappings documented under :h ]] and :h ]m for the python language. The mappings are:

]] Jump forward to begin of next toplevel
[[ Jump backwards to begin of current toplevel (if already there, previous toplevel)
]m Jump forward to begin of next method/scope
[m Jump backwords to begin of previous method/scope

][ Jump forward to end of current toplevel
[] Jump backward to end of previous of toplevel
]M Jump forward to end of current method/scope
[M Jump backward to end of previous method/scope

Following example source code with comments illustrates the different mappings

class Mapping:                              # [[[[
    def __init__(self, iterable):
        pass

    def update(self, iterable):
        pass

    __update = update                       # []

class Reverse:                              # [[ or [m[m
    def __init__(self, data):               # [m
        self.data = data
        self.index = len(data)              # [M

    def __iter__(self):                     # <--- CURSOR
        return self                         # ]M

    def __next__(self):                     # ]m
        if self.index == 0:
            raise StopIteration
        self.index = self.index - 1
        return self.data[self.index]        # ][

class MappingSubclass(Mapping):             # ]] or ]m]m

    def update(self, keys, values):
        pass

The mappings have been added and improved in the commits abd468ed0 (2016-09-08), 01164a6546b4 (2017-11-02), and 7f2e9d7c9cd (2017-11-11).

If you do not have the new version of this file yet, you can download it and put it into ~/.vim/ftplugin/python.vim. This folder takes precedence before $VIMRUNTIME/ftplugin.

Before these mappings have been added to $VIMRUNTIME, there has been the plugin python-mode which provides [[, ]], [M, and ]M. In addition python-mode also defines the text objects aC, iC, aM, and iM:

Plugin python-mode

This vim plugin provides motions similar to built-in ones:

2.4 Vim motion ~
                                                                *pymode-motion*

Support Vim motion (See |operator|) for python objects (such as functions,
class and methods).

`C` — means class
`M` — means method or function
                                                            *pymode-motion-keys*

==========  ============================
Key         Command (modes)
==========  ============================
[[          Jump to previous class or function (normal, visual, operator)
]]          Jump to next class or function  (normal, visual, operator)
[M          Jump to previous class or method (normal, visual, operator)
]M          Jump to next class or method (normal, visual, operator)
aC          Select a class. Ex: vaC, daC, yaC, caC (normal, operator)
iC          Select inner class. Ex: viC, diC, yiC, ciC (normal, operator)
aM          Select a function or method. Ex: vaM, daM, yaM, caM (normal, operator)
iM          Select inner func. or method. Ex: viM, diM, yiM, ciM (normal, operator)
==========  ============================

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. In addition, this plugin defines the text objects ic/ac (class), if/af (function), id/ad (docstring).

Neovim & nvim-treesitter-textobjects

For neovim, you can use treesitter and the neovim plugin nvim-treesitter-textobjects.

Textobjects if/af & ic/ac

These are not contained in $VIMRUNTIME/ftplugin/python.vim but are provided by a few plugins

For a discussion about textobjects for python see what's the fastest way to select a function of Python via VIM?.

Solution 2

python.vim

Makes it much easier to navigate around python code blocks.

Shortcuts:

  • ]t -- Jump to beginning of block
  • ]e -- Jump to end of block
  • ]v -- Select (Visual Line Mode) block
  • ]< -- Shift block to left
  • ]> -- Shift block to right
  • ]# -- Comment selection
  • ]u -- Uncomment selection
  • ]c -- Select current/previous class
  • ]d -- Select current/previous function
  • ]<up> -- Jump to previous line with the same/lower indentation
  • ]<down> -- Jump to next line with the same/lower indentation

python_match.vim

extends %:

  • % - cycle through if/elif/else, try/except/catch, for/continue/break
  • g% - move opposite of %
  • [% - move to the beginning of the current code block
  • ]% - move to the end of the current code block

All the above motions work with Normal, Visual, and Operator-pending modes, so:

  • d]% - delete until the end of the current block
  • v]%d - should do the same, going through Visual mode so that you can see what is being deleted
  • V]%d - above, but with line selection

Solution 3

It's very easy to move indented blocks when you have set foldmethod=indent. For example, if you're on the def main(): line in the following snippet:

def main():
+-- 35 lines: gps.init()-----------------------------------------------------

if __name__ == "__main__": main()

then dj takes the whole main function and it can be pasted elsewhere.

Share:
20,867
Admin
Author by

Admin

Updated on October 28, 2020

Comments

  • Admin
    Admin over 3 years

    Given a python file with the following repeated endlessly:

    def myFunction(a, b, c):
        if a:
            print b
        elif c:
            print 'hello'
    

    I'd like to move around and edit this file using familiar vim movements. For instance, using (, ), [[, ]], {, } or deleting/yanking/changing text using commands like di}.

    In other languages (like C++, Java, C#, etc) you've got curly brackets abound, so using a movement like di} can easily find a matching curly brace and act on that block. And in fact if I am on the 'b' character on the above text and do a di) in vim, it successfully deletes the text between the two parens.

    The issue is in python's detection of code blocks, I think. Using (, ), [[, ]], {, or } as movements all pretty much do the same thing, bringing you to the start (above or on the def line) or end (after the last line of the function) of the function. And there is no way, as far as I know, to easily tell vim "select everything for this indentation block." In the above example, I'd like to be on in 'i' of the if line, type di} and have it delete the entire if block (to the end of this particular function).

    I'm sure it should be possible to tell vim to operate on an indentation basis for such movements (well, maybe not that particular movement, but some user defined action). Any thoughts on how to accomplish this?

  • shivams
    shivams over 7 years
    In 2016, this is the standard answer.
  • clog14
    clog14 almost 4 years
    Hi: thanks for the answer. Just real quick: is there a way that the first method correctly deals with decorators?
  • Hotschke
    Hotschke almost 4 years
    @clog14 Sorry, I do not know the answer. If you mean by the first method the mappings given in $VIMRUNTIME/ftplugin/python.vim, I would suggest that you raise an issue in the upstream repository under github.com/tpict/vim-ftplugin-python. Give an example file and a description of the behavior you expect for the different square bracket mappings.