Edit1. Take two. Trying to absorb the feedback from Luc. Without temp file (readfile()
turned out to be not available in VIM 6.x I have on some systems).
: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
can't be used because it works line-wise. I replaced <Esc>...<Insert>
with the all better <C-O>
. I left redraw in, as it helps for the cases of called command produces output to the stderr.
Or using <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>
Also what is the difference between function!
and function
?
Exclamation on the end of command most of the time means force to execute. (Looking in the :help
is advised since different commands use !
differently, but VIM tries to document all forms of the commands.) In the case of the function
it tells VIM to override previous definition of the function. E.g. if you put the code above into the func1.vim
file, first time :source func1.vim
would work fine, but the second time it would fail with error that function InsertCmd is already defined.
I did once before try to implement something similar here. I'm not good at VIM programming, thus it looks lame and the suggestion from Luc should take precedence.
Here it goes anyway:
: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>
Despite being lame, it works though.