到目前为止我所拥有的:
function! GetMarker()
return system('echo $random `date` | md5sum | cut -d" " -f1')
endfunction
Run Code Online (Sandbox Code Playgroud)
我希望能够做到:getmarker
并让它在我的光标处插入该系统命令的输出,没有新行.
此外之间有什么区别function!
和function
?
编辑:在你问任何人之前,我需要随机字符串来标记我的代码中的部分,以便我可以通过在我的todo wiki中引用我的笔记再次找到它们.
您可以修剪/与的Chomp最后换行符matchstr()
,substitute
,[:-2]
,等
function s:GetMarker()
let res = system('echo $random `date` | md5sum | cut -d" " -f1')
" then either
let res = matchstr(res, '.*\ze\n')
" or
let res = res[:-2]
" or
let res = substitute(res, '\n$', '', '')
return res
endfunction
command! -nargs=0 GetMarker put=s:GetMarker()
Run Code Online (Sandbox Code Playgroud)
敲击函数/命令定义(带'!')将允许您多次定义脚本,从而更新您正在维护的函数/命令而无需退出vim.
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>
Run Code Online (Sandbox Code Playgroud)
:put
不能使用,因为它按行工作.我更换<Esc>...<Insert>
了一切更好<C-O>
.我离开了重绘,因为它有助于被调用命令的情况产生输出到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>
Run Code Online (Sandbox Code Playgroud)
此外之间有什么区别
function!
和function
?
命令结束时的感叹号大部分时间意味着强制执行.(:help
因为不同的命令使用!
不同,所以建议查看,但VIM会尝试记录所有形式的命令.)在它的情况下,function
它告诉VIM覆盖函数的先前定义.例如,如果您将上面的代码放入func1.vim
文件中,第一次:source func1.vim
可以正常工作,但第二次它将失败并且已经定义了函数InsertCmd的错误.
在尝试实现类似的东西之前,我曾做过一次.我不擅长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>
Run Code Online (Sandbox Code Playgroud)
虽然是跛脚,但它有效.
归档时间: |
|
查看次数: |
5411 次 |
最近记录: |