gpt4 book ai didi

python - 如果尚未导入,则 Vim 函数插入 Python 导入

转载 作者:太空宇宙 更新时间:2023-11-03 15:53:01 25 4
gpt4 key购买 nike

我目前有一个 Vim 函数,可以将选定的文本包装在 time.time() block 中,这样我就可以快速计时。

我希望该函数也转到文件顶部,检查 import time 是否存在,只有在不存在时才插入 import time尚不存在。

有什么方法可以检查 Vim 中是否存在文本?

最佳答案

这是我目前拥有的。它有效,但如果您有更好的解决方案,请发布您自己的解决方案!

另外,请注意具有 ^M 的行是通过按 Ctrl-V 然后在插入模式下按 Enter 按钮形成的(Stack Overflow 没有很好地复制它)。

" easily wrap the selected text in a time.time() statement for quick timing
fun! s:PythonTiming(line1, line2)

" mark line one && keep track of lines selected
execute 'normal!' 'me'
let l:numDiff = a:line2 - a:line1

" start timing
execute 'normal!' 'Ostart = time.time()'

" end timing
while line('.') < a:line2 + 1
execute 'normal!' 'j'
endwhile
execute 'normal!' 'oend = time.time()'
execute 'normal!' 'oprint; print("end - start: "); print(end - start)'

" add the `import time` statement if not already imported
let match = search('import time', 'nw')
if match == 0
silent! execute 'normal!' 'gg/import/^M'
execute 'normal!' 'oimport time'
endif

" go back to the initial mark
execute 'normal!' '`e'

endfun
command! -range Time :call s:PythonTiming(<line1>, <line2>)

关于python - 如果尚未导入,则 Vim 函数插入 Python 导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45376398/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com