Edit1。拿兩個。試圖吸收Luc的反饋。沒有臨時文件(readfile()
原來在VIM 6.x中不可用,我在某些系統上)。
:function InsertCmd(cmd)
: let l = system(a:cmd)
: let l = substitute(l, '\n$', '', '')
: exe "normal a".l
: redraw!
:endfunction
:imap <silent> <F5> <C-O>:call InsertCmd('echo date \| md5sum \| cut -d" " -f1')<CR>
:map <silent> <F5> :call InsertCmd('echo date \| md5sum \| cut -d" " -f1')<CR>
:put
不能使用,因爲它工作在行方式。我用<C-O>
代替<Esc>...<Insert>
。我離開了重繪,因爲它有助於調用命令生成輸出到stderr的情況。
或者用<C-R>=
:
:function InsertCmd(cmd)
: let l = system(a:cmd)
: let l = substitute(l, '\n$', '', '')
: return l
:endfunction
:imap <silent> <F5> <C-R>=InsertCmd('echo date \| md5sum \| cut -d" " -f1')<CR>
也就是function!
和function
之間的區別?
大多數時間在命令結束時感嘆意味着強制執行。 (建議查看:help
,因爲不同的命令使用!
的方式不同,但VIM會嘗試記錄所有形式的命令。)對於function
,它告訴VIM覆蓋此函數的先前定義。例如。如果將上面的代碼放入func1.vim
文件中,則第一次:source func1.vim
可以正常工作,但第二次將失敗,並且函數InsertCmd已被定義。
我曾嘗試實施過一次something similar here。我不擅長VIM編程,因此看起來很蹩腳,Luc的建議應該優先考慮。
這裏去反正:
:function InsertCmd(cmd)
: exe ':silent !'.a:cmd.' > /tmp/vim.insert.xxx 2>/dev/null'
: let l = readfile('/tmp/vim.insert.xxx', '', 1)
: exe "normal a".l[0]
: redraw!
:endfunction
:imap <silent> <F5> <Esc>:call InsertCmd('hostname')<CR><Insert>
:map <silent> <F5> :call InsertCmd('hostname')<CR>
儘管是瘸腿的,它的工作原理雖然。
如果您的方法有一個問題,它與臨時文件有關。多個用戶不能使用相同的腳本。最好使用tempname()。否則,可能會增強一些內容: - 不重繪, - 返回l [0], - 並將其插入' ='以避免與光標位置有關的錯誤。 –
2010-07-09 09:58:49
@Luc:' =' - 非常非常酷。 'tempname()' - 很好的瞭解。感謝您的建議。 –
Dummy00001
2010-07-09 12:23:29
「命令結束時的感嘆號意味着強制執行。」這不是真的。有很多反例。見':g',':delm',':put',... – 2010-07-12 15:36:24