Vim function to insert a Python import if it hasn't been imported yet -
i have vim function wrap selected text within time.time() block can time things.
i function go top of file, check if import time exists or not, , insert import time if doesn't exist already.
is there way check if text exists or not in vim?
this have. works, please post own solution if have better!
also, note line has ^m formed pressing ctrl-v , enter button in insert mode (stack overflow doesn't copy on well).
" wrap selected text in time.time() statement quick timing fun! s:pythontiming(line1, line2) " mark line 1 && 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 `import time` statement if not imported let match = search('import time', 'nw') if match == 0 silent! execute 'normal!' 'gg/import/^m' execute 'normal!' 'oimport time' endif " go initial mark execute 'normal!' '`e' endfun command! -range time :call s:pythontiming(<line1>, <line2>)
Comments
Post a Comment