Sunday, March 30, 2008

Inserting command output from a function

How would I use a vim function to insert the output of these commands at the current cursor position?

Well, that would look like a crude hack, yet here it goes:

:function InsertCmd( cmd )
: exe ':silent !'.a:cmd.' > /tmp/ 2>/dev/null'
: let l = readfile( '/tmp/', '', 1 )
: exe "normal a".l[0]
: redraw!

:imap <silent> <F5> <Esc>:call InsertCmd( 'hostname' )<CR><Insert>
:map <silent> <F5> :call InsertCmd( 'hostname' )<CR>

The function InsertCmd(cmd) runs in shell given command and redirects its stdout to a file. Then using readfile() function, first line of file is read into list l. Finally using :normal command and a (:help a) command we put the first line of output into buffer.

The two mappings for F5 key - for insert and for command mode show how to invoke the function to insert output of "hostname" into current cursor position. ":imap" probably needs extra <Right> on the end since due to switch between insert/normal modes cursor goes one position back.

Edit1. Better way to do it, from SO.