VIM + Python - "gd"命令无法正常工作

Eda*_*aor 5 python ide vim editor

我开始使用VIM编写Python程序.我遇到了一些问题,希望有人可以帮我解决这个问题.

"gd"命令应该将您带到第一个在当前函数中定义/使用变量的位置.根据我的理解,它与"[["转到函数顶部,然后执行搜索变量名称相同.

问题是,当我在Python函数中尝试这个时,vim会在整个文件中找到第一个变量.

有关为什么会发生这种情况的想法/如何解决这个问题?

DrA*_*rAl 3

我认为问题出在 Vim 处理函数的方式上。从文档中[[

                            *[[*
[[          [count] sections backward or to the previous '{' in
            the first column.  |exclusive|
            Note that |exclusive-linewise| often applies.
Run Code Online (Sandbox Code Playgroud)

除非在某个地方专门为 python 文件定义了一个部分(我不相信这是可能的,因为它们应该是两个字母的 nroff 部分),否则这将假设第一列中应该有一个开括号,这与 python 文件无关。

我建议在 Vim 邮件列表上询问是否有任何插件或解决方法。或者,您可以定义如下映射:

nmap gd :let varname = '\<<C-R><C-W>\>'<CR>?\<def\><CR>/<C-R>=varname<CR><CR>
Run Code Online (Sandbox Code Playgroud)

这可以通过函数更优雅地完成,但这只是一个应该可行的快速技巧。它映射gd到一个函数,该函数设置变量“varname”来保存光标所在的单词,向后搜索 def,然后向前搜索该变量:

    :let varname =             " Variable setting
    '\<                        " String start and word boundary
    <C-R><C-W>                 " Ctrl-R, Ctrl-W: pull in the word under the cursor
    \>'                        " Word boundary and string end
    <CR>                       " Enter - finish this command
    ?                          " Search backwards for...
    \<def\>                    " def but not undefined etc (using word boundaries)
    <CR>                       " Enter - Perform search
    /                          " Now search forward
    <C-R>=                     " Pull in something from an expression
    varname<CR>                " The expression is 'varname', so pull in the contents of varname
    <CR>                       " Enter - perform search
Run Code Online (Sandbox Code Playgroud)