【发布时间】:2017-07-28 14:46:13
【问题描述】:
我目前有一个 Vim 函数,它将选定的文本包装在 time.time() 块中,以便我可以快速计时。
我希望该函数也转到文件顶部,检查import time 是否存在,并仅在import time 不存在时插入它。
有什么方法可以检查 Vim 中是否存在文本?
【问题讨论】:
我目前有一个 Vim 函数,它将选定的文本包装在 time.time() 块中,以便我可以快速计时。
我希望该函数也转到文件顶部,检查import time 是否存在,并仅在import time 不存在时插入它。
有什么方法可以检查 Vim 中是否存在文本?
【问题讨论】:
这是我目前拥有的。它有效,但如果您有更好的解决方案,请发布您自己的解决方案!
另外,请注意具有^M 的行是通过在插入模式下按Ctrl-V 然后按Enter 按钮形成的(堆栈溢出不能很好地复制它)。
" 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>)
【讨论】: