不污染更改列表的vim脚本搜索功能(撤消)

不污染更改列表的vim脚本搜索功能(撤消),vim,vim-plugin,ultisnips,Vim,Vim Plugin,Ultisnips,所以我用python插值编写了一个小的ulti-snips片段。 通常,当您点击“撤消”按钮时,在展开代码段后,它会返回到触发字。但在这种情况下,我必须按下撤销键两次。我怀疑这是我正在使用的搜索功能。我真的很感谢你在这方面的帮助。我想要么使用比搜索更好的功能,要么以某种方式使用搜索(或任何导致此问题的原因),以避免污染撤消历史记录。 以下是片段: snippet super "Adds a super function for the current function" b `!p import

所以我用python插值编写了一个小的ulti-snips片段。 通常,当您点击“撤消”按钮时,在展开代码段后,它会返回到触发字。但在这种情况下,我必须按下撤销键两次。我怀疑这是我正在使用的搜索功能。我真的很感谢你在这方面的帮助。我想要么使用比搜索更好的功能,要么以某种方式使用搜索(或任何导致此问题的原因),以避免污染撤消历史记录。 以下是片段:

snippet super "Adds a super function for the current function" b
`!p
import vim
# get the class name
line_number = int(vim.eval('search("class .*(", "bn")'))
line = vim.current.buffer[line_number - 1]
class_name = re.findall(r'class\s+(.*?)\s*\(', line)[0]
# get the function signature
line_number = int(vim.eval('search("def.*self.*", "bn")'))
line = vim.current.buffer[line_number - 1]
func = re.findall(r'def\s+(.*):', line)[0]
matches = re.findall(r'(.*)\(self,?\s*(.*)\)', func)
snip.rv = 'super(%s, self).%s(%s)' % (class_name, matches[0][0], matches[0][1])
`
endsnippet

您可以完全在python中使用文本。python比vim脚本更强大。以下是我的例子:

buf = vim.current.buffer
line_number = vim.current.window.cursor[0] - 1 # cursor line start from 1. so minus it
previous_lines = "\n".join(buf[0:line_number])

try:
    class_name = re.findall(r'class\s+(.*?)\s*\(', previous_lines)[-1]
    func_name, func_other_param = re.findall(r'def\s+(.*)\(self,?\s*(.*)?\):', previous_lines)[-1]
    snip.rv = 'super(%s, self).%s(%s)' % (class_name, func_name, func_other_param)
except IndexError as e:
    snip.rv = 'super'    # regex match fail.

您可以使用
vim.eval('search
,它将移动光标。这是导致您撤消两次的原因。您可以通过
content=“\n”。join(vim.current.buffer)获取当前缓冲区内容
。然后用python完成这项工作。我在命令中添加了一个n标志,这样它就不会移动光标。有更好的搜索方法吗?谢谢,是的,python中的所有东西都绝对是更好的方法!