2013-02-08 44 views
0

我想寫一個函數來排序Scala導入。如何從函數執行Vim的排序命令?

我已經成功找到開始,其中進口的結束。現在我只需要做實際的排序。

function! SortScalaImport() 
    call cursor(1, 1) 
    let start = search('^import') "find first line with import 
    let end = search('^\(import\|\n\)\@!') "find first non-import line 
    let end = end - 1 
    execute 'normal '.start.','.end.'sort' 
endfunction 

函數的最後一行應該做,但什麼都不做。我哪裏錯了?

回答

2

因爲Vim是基於模式的編輯器,你還需要腳本時要考慮的模式。

:normal正常模式命令,即短的東西來導航(jw)或變化(xgUU)文本。隨着:,您輸入命令行模式,在Ex命令(如:substitute或您的:sort)被執行。通常情況下,你可以把這些作爲,是爲你的Vimscript函數(例如%delete _),但是當你想要把變量(在你的情況:行號)進去,你需要:execute它與一切,但變量'quoted'

1

我不知道爲什麼你在執行添加normal

這個工作適合你嗎?

execute start.','.end.' sort'