douzhiji2020 2013-09-21 10:13
浏览 72
已采纳

Vim中Golang的缩写Import and Drop命令

I've used Vim to code my Go script for a month, and I'm pretty comfortable with :Import and :Drop vim command when it comes to importing and unimporting any packages.

However, I've more and more tired of typing such a long word which includes Capital letter so frequently, so I came to think that if I can use :i and :d other than :Import and :Drop, I'd be satisfied even more. However, I'm not sure whether I can define such a command in Vim, since it involves 1) turning into command-line mode, and 2) taking a variable which is determined dynamically in typing.

So for example, if I import encoding/csv package, all I want to type in is :i encoding/csv, etc... Can I define those commands?

I stumbled upon this question, but I cannot get what it is doing nor I'm not sure whether the answer applies to this question in the first place. Can I solve the issue by replacing all of w and W on the linked post to i or d?

Also, does it differ from the case that doesn't take a variable (say, Fmt command to go-format the script)?

Thanks.

  • 写回答

3条回答 默认 最新

  • dongzheng8463 2013-09-21 11:15
    关注

    EDIT 2: As pointed out by glts below, it's better to use cnoreabbrev or cnorea as pointed out in this answer.

    Better working example (paste into ~/.vimrc):

    cnorea <expr> i ((getcmdtype() is# ':' && getcmdline() is# 'i')?('Import'):('i'))
    cnorea <expr> d ((getcmdtype() is# ':' && getcmdline() is# 'd')?('Drop'):('d'))
    

    EDIT: Simple answer. Just using cabbrev or ca (command abbreviation) seems to work:

    WORKING EXAMPLE (Paste this in ~/.vimrc):

    ca i Import
    ca d Drop
    

    Working on vim 7.3, Ubuntu 64 bit.

    ORIGINAL ANSWER (more complex):

    According to http://vim.wikia.com/wiki/Replace_a_builtin_command_using_cabbrev :

    You can use :command to define your own commands, but user-defined commands must start with an uppercase letter to avoid confusion with built-in commands.

    So, using :command, you can probably use :I and :D, but not :i and :d.

    It goes on to say:

    Suppose you have a user-defined :E command that you want to use to override the default :e command. You could do the following:

    :cabbrev e <c-r>=(getcmdtype()==':' && getcmdpos()==1 ? 'E' : 'e')<CR>
    

    The (getcmdtype()==':' && getcmdpos()) makes sure the replacement happens only in the first column of the command line (i.e. not later in the line, where it is most likely NOT intended to be used as a command, and not on the search line, which is also affected by cabbrev).
    If you do this a lot, it would be useful to define a function to do it for you. Use this to quickly and easily define lowercase abbreviations for whatever command you want:

    function! CommandCabbr(abbreviation, expansion)
      execute 'cabbr ' . a:abbreviation . ' <c-r>=getcmdpos() == 1 && getcmdtype() == ":" ? "' . a:expansion . '" : "' . a:abbreviation . '"<CR>'
    endfunction
    command! -nargs=+ CommandCabbr call CommandCabbr(<f-args>)
    " Use it on itself to define a simpler abbreviation for itself.
    CommandCabbr ccab CommandCabbr
    

    This not only creates the function, but also provides the (lowercase!) command :ccab to define such abbreviations "on the fly".

    So using a function looks like one way to go if you want to use lowercase :i and :d.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟