2017-07-28 2 views
3

私は現在、選択されたテキストをtime.time()ブロック内にラップして、すばやく時間を計ることができるVim関数を持っています。まだインポートされていない場合、Pythonインポートを挿入するためのVim関数

また、ファイルの先頭に移動して、import timeが存在するかどうかを確認し、まだ存在しない場合はimport timeを挿入してください。

Vimにテキストが存在するかどうかを確認する方法はありますか?

答えて

3

これは私の現在のものです。それは動作しますが、あなたが何か良いものを持っていれば、あなた自身の解決策を投稿してください!

また、Ctrl-VEnterボタンを挿入モードで押すと、^Mの行が形成されることに注意してください(スタックオーバーフローではそれをうまくコピーできません)。

" 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>) 
関連する問題