在 vim 中编辑 python 文件的更有效的动作 [英] More efficient movements editing python files in vim

查看:33
本文介绍了在 vim 中编辑 python 文件的更有效的动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个 python 文件,其中无限重复:

Given a python file with the following repeated endlessly:

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

我想使用熟悉的 vim 移动来移动并编辑此文件.例如,使用 (, ), [[, ]], {, } 或使用 di} 等命令删除/拉取/更改文本.

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}.

在其他语言(如 C++、Java、C# 等)中,大括号比比皆是,因此使用像 di} 这样的移动可以轻松找到匹配的大括号并在该块上执行操作.事实上,如果我在上面文本的 'b' 字符上并在 vim 中做一个 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.

我认为问题在于python对代码块的检测.使用 (, ), [[, ]], {, or } 作为动作几乎都做同样的事情,把你带到开始(在 def 行之上或在 def 行上)或结束(在函数的最后一行之后)功能.据我所知,没有办法轻松告诉vim选择此缩进块的所有内容".在上面的例子中,我想在 if 行的 'i' 中,输入 di} 并让它删除整个 if 块(到这个特定函数的末尾).

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

我确信应该可以告诉 vim 在缩进的基础上进行此类运动(好吧,也许不是那个特定的运动,而是一些用户定义的动作).关于如何实现这一点的任何想法?

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?

推荐答案

方括号映射 [[, ]], [m,]m 和类似的

$VIMRUNTIME/ftplugin/python.vim now (2018) 为 python 语言重新映射 :h ]]:h ]m 下记录的所有内置映射.映射是:

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

已在提交中添加和改进映射abd468ed0 (2016-09-08),0164a635daf96a1f17d1cb2d07f32a66> (2017-11-02) 和 7f2e9d7c9cdfc5201a899b(2017-11-11).

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

如果你还没有这个文件的新版本,你可以下载它并放入~/.vim/ftplugin/python.vim.此文件夹优先于 $VIMRUNTIME/ftplugin.

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.

在这些映射被添加到 $VIMRUNTIME 之前,已经有插件 python-mode 提供 [[, ]][M]M.另外python-mode还定义了文本对象aCiCaMiM:

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:

这个 vim 插件提供类似于内置的动作:

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

插件Pythonsense

这个插件提供了类似的动作,但略有修改:

Plugin Pythonsense

This plugin provides similar motions but slightly modified:

Vim 8.0 的库存类"动作(]]"、[["等),查找从第一列开始的块,无论这些块是类还是功能块,而其方法/function motions ("[m", "]m" 等) 查找任何缩进处的所有块,而不管这些块是类块还是功能块.相比之下,Pythonsense"类动作用于查找所有且仅类定义,无论其缩进级别如何,而其方法/函数动作用于查找所有且仅方法/函数定义,无论其缩进级别如何.

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.

所有细节和例子都在 https://github.com/jeetsukumaran/vim-pythonsense#stock-vim-vs-pythonsense-motions.另外,这个插件定义了文本对象ic/ac(类)、if/af(函数)、id/ad(docstring).

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

有关 Python 文本对象的讨论,请参见 通过 VIM 选择 Python 函数的最快方法是什么?.

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

这篇关于在 vim 中编辑 python 文件的更有效的动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆