2011-11-02 50 views
4

後的光標位置我試圖用一個簡單的函數來呼應XML標籤:VIM函數生成一個標記,如何中心輸出

func! SayTag() 
    let tagName = input("Tag: ") 
    return "<" . tagName . ">" . "<" . tagName . ">" 
endfunc 

並綁定到:

imap \tag <C-R>=SayTag()<CR>

但輸出後,光標位於標籤之後,例如:<TAG> </TAG> _CURSOR_

如何動態設置光標位置?

回答

2

我不很喜歡下面的解決方案,但我正在探索你的問題一點,因爲我不能想到一個簡單的解決方案:

func! GetTag() 
    call inputsave() 
    let g:tagName = input("Tag: ") 
    call inputrestore() 
endfunc 

imap \t <esc>:call GetTag()<CR>:exe "normal! i<".tagName."></".tagName.">"<CR>bba 

應該做工精細,你可以閱讀文檔here (見最新的例子)。順便說一句,如果你打算寫很多XML或HTML的,我建議你看一看以下插件:

他們將節省你很多打字。

2

另一個可能的實現,使用一個更好的地圖。

function! GetTag() 
    let tag = input("Tag: ") 
    execute "normal! i<".tag."></".tag.">" 
    execute "normal! " . repeat('h', strlen(tag)+2) 
endfunction 

inoremap \tag <C-o>:call GetTag()<enter> 

但是,我非常贊同使用那些用於這類事情的插件來節省大量時間。

編輯:刪除不必要的循環。

+0

哦不錯的實現:) – lucapette