dongtu1789 2017-08-17 04:16
浏览 120
已采纳

将vim外部命令结果行存储到VimL数组中,以进行一些自定义自动完成

This little snippet aims to make a little autocomplete script in VimL. the first code:

cat % | grep function | sed  's/(/ /g' | awk '{print $3}'

list all the method inside a php class. For example the output of this command could be:

__construct
__toString
foo
bar

If I send to complete function an array

call complete(col('.'), ["__construct", "__toString", "foo", "bar"])

I can see this menu:

enter image description here

So the final question is, ... how can I transform this:

__construct
__toString
foo
bar

into this ["__construct", "__toString", "foo", "bar"]?

Here my wrong code:

inoremap <F5> <C-R>=CustomComplete()<CR>
func! CustomComplete()
    let l:functions = system("cat % | grep function | sed  's/(/ /g' | awk '{print $3}'")
    call complete(col('.'), l:functions)
    return ''
endfunc
  • 写回答

1条回答 默认 最新

  • doutu9810 2017-08-17 05:15
    关注

    Refering to the current file

    First, the % symbol is only parsed in the command-line. With system(), you need to expand it on your own:

    system("cat " . expand('%') . " | grep function | sed  's/(/ /g' | awk '{print $3}'")
    

    Now, there can be special characters in the current filename. To be safe, wrap the result in shellescape():

    system("cat " . shellescape(expand('%')) . " | grep function | sed  's/(/ /g' | awk '{print $3}'")
    

    Turning external command output into a List

    You can use the split() function with a {pattern} of (newline):

    let l:functions = split(
    \   system("cat " . shellescape(expand('%')) . " | grep function | sed  's/(/ /g' | awk '{print $3}'"),
    \   '
    '
    \)
    

    Recent Vim versions have systemlist(), just for this common use case:

    let l:functions = systemlist("cat " . shellescape(expand('%')) . " | grep function | sed  's/(/ /g' | awk '{print $3}'")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥30 成都市武侯区住宅小区兴趣点
  • ¥15 数值分析的小论文,十页内
  • ¥15 Windows软实时
  • ¥15 自有服务器搭建网络隧道并且负载均衡
  • ¥15 opencv打开dataloader显示为nonetype
  • ¥15 MacOS 80端口外网无法访问
  • ¥50 js逆转反解密-会的来
  • ¥15 wrodpress如何调取数据库并展示
  • ¥15 python梯形积分与GPS测得位移使用卡尔曼滤波融合问题
  • ¥15 匈牙利算法分割求损失问题
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部