加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何编写vim函数来输出系统命令的结果?

发布时间:2020-12-15 22:12:01 所属栏目:安全 来源:网络整理
导读:到目前为止我所拥有的: function! GetMarker() return system('echo $random `date` | md5sum | cut -d" " -f1')endfunction 我希望能够做一个:getmarker并让它在我的光标处插入该系统命令的输出,没有新行. 还有什么区别功能!和功能? 编辑:在你问任何人
到目前为止我所拥有的:

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! and function?

命令结束时的感叹号大部分时间意味着强制执行. (看看:建议帮助,因为不同的命令使用!不同,但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>

虽然是跛脚,但它有效.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读