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 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记