到目前为止我所拥有的:
function! GetMarker() return system('echo $random `date` | md5sum | cut -d" " -f1') endfunction
我希望能够做一个:getmarker并让它在我的光标处插入该系统命令的输出,没有新行.
编辑:在你问任何人之前,我需要随机字符串来标记我的代码中的部分,以便我可以通过在我的todo wiki中引用我的笔记再次找到它们.
解决方法
EDIT1.拿两个.试图吸收吕克的反馈.没有临时文件(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不能使用因为它按行工作.我替换了< Esc> …< Insert>与所有更好的< C-O>.我离开了重绘,因为它有助于被调用命令的情况产生输出到stderr.
或使用< C-R> =:
:function InsertCmd( cmd ) : let l = system( a:cmd ) : let l = substitute(l,'') : return l :endfunction :imap <silent> <F5> <C-R>=InsertCmd( 'echo date \| md5sum \| cut -d" " -f1' )<CR>
Also what is the difference between
function!
andfunction
?
命令结束时的感叹号大部分时间意味着强制执行. (看看:建议帮助,因为不同的命令使用!不同,但VIM尝试记录所有形式的命令.)在函数的情况下,它告诉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>
虽然是跛脚,但它有效.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。