在Vim中缩写Golang的导入和删除命令。

huangapple go评论97阅读模式
英文:

Abbreviate Import and Drop command of Golang in Vim

问题

我已经使用Vim编写Go脚本一个月了,对于导入和取消导入任何包时使用的:Import:Drop命令我感到非常舒适。

然而,我越来越厌倦频繁输入包含大写字母的这么长的单词,所以我想如果我可以使用:i:d代替:Import:Drop,我会更加满意。但是,我不确定是否可以在Vim中定义这样的命令,因为它涉及到1)进入命令行模式,和2)在输入时动态确定的变量。

所以例如,如果我导入encoding/csv包,我只想输入:i encoding/csv,等等...我可以定义这些命令吗?

我偶然发现了这个问题,但我不明白它在做什么,也不确定答案是否适用于这个问题。我可以通过将链接帖子中的所有wW替换为id来解决这个问题吗?

另外,这与不带变量的情况有什么不同(比如,Fmt命令用于格式化脚本)?

谢谢。

英文:

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.

答案1

得分: 2

编辑2:如下所指出的glts,最好使用cnoreabbrevcnorea,如此答案所指出。

更好的工作示例(粘贴到~/.vimrc中):

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

编辑:简单的答案。只需使用cabbrevca(命令缩写)似乎可以工作:

工作示例(将其粘贴到~/.vimrc中):

ca i Import
ca d Drop

在vim 7.3,Ubuntu 64位上工作。

原始答案(更复杂):

根据http://vim.wikia.com/wiki/Replace_a_builtin_command_using_cabbrev:

您可以使用:command来定义自己的命令,但是用户定义的命令必须以大写字母开头,以避免与内置命令混淆。

因此,使用:command,您可能可以使用:I:D,但不能使用:i:d

它继续说道:

假设您有一个用户定义的:E命令,您希望使用它来覆盖默认的:e命令。您可以执行以下操作:

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

(getcmdtype()==':' && getcmdpos())确保替换仅发生在命令行的第一列(即不是在行的后面,那里最有可能不打算将其用作命令,也不是在搜索行上,该行也受到cabbrev的影响)。
如果您经常这样做,定义一个函数来为您执行此操作将非常有用。使用此函数可以快速轻松地为您想要的任何命令定义小写缩写:

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

这不仅创建了函数,还提供了(小写!)命令:ccab来“即时”定义此类缩写。

因此,如果您想使用小写的:i:d,使用函数似乎是一种方法。

英文:

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 &lt;expr&gt; i ((getcmdtype() is# &#39;:&#39; &amp;&amp; getcmdline() is# &#39;i&#39;)?(&#39;Import&#39;):(&#39;i&#39;))
cnorea &lt;expr&gt; d ((getcmdtype() is# &#39;:&#39; &amp;&amp; getcmdline() is# &#39;d&#39;)?(&#39;Drop&#39;):(&#39;d&#39;))

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 &lt;c-r&gt;=(getcmdtype()==&#39;:&#39; &amp;&amp; getcmdpos()==1 ? &#39;E&#39; : &#39;e&#39;)&lt;CR&gt;

>>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 &#39;cabbr &#39; . a:abbreviation . &#39; &lt;c-r&gt;=getcmdpos() == 1 &amp;&amp; getcmdtype() == &quot;:&quot; ? &quot;&#39; . a:expansion . &#39;&quot; : &quot;&#39; . a:abbreviation . &#39;&quot;&lt;CR&gt;&#39;
endfunction
command! -nargs=+ CommandCabbr call CommandCabbr(&lt;f-args&gt;)
&quot; 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

得分: 0

cmdalias.vim - 为 Vim 命令创建别名 插件允许你设置小写别名,例如:

:Alias i Import
英文:

The cmdalias.vim - Create aliases for Vim commands plugin allows you to set up lowercase aliases, e.g.:

:Alias i Import

答案3

得分: 0

一个简单的解决方案:

nnoremap <leader>i :Import<Space>
nnoremap <leader>d :Drop<Space>

在正常模式下,<leader>i会在命令行中填充以下内容:

:Import | <--- 光标

然后你可以输入参数或使用<tab>进行自动补全。

更好的解决方案:

nnoremap <leader>i :Import <C-d>
nnoremap <leader>d :Drop <C-d>

<leader>i会在命令行中填充以下内容:

:Import | <--- 光标

并显示可能的补全列表。

更好的解决方案:

set wildcharm=<C-z>
nnoremap <leader>i :Import <C-z>
nnoremap <leader>d :Drop <C-z>

假设你已经设置好了wildmenu<leader>i会在命令行中填充以下内容:

:Import 列表中的第一个包名

同时打开了wildmenu,可以使用<tab>进行补全。

英文:

A simple solution:

nnoremap &lt;leader&gt;i :Import&lt;Space&gt;
nnoremap &lt;leader&gt;d :Drop&lt;Space&gt;

In normal mode, &lt;leader&gt;i populates the command-line with

:Import | &lt;--- cursor

ready for you to type in or &lt;tab&gt;-complete an argument.

Better:

nnoremap &lt;leader&gt;i :Import &lt;C-d&gt;
nnoremap &lt;leader&gt;d :Drop &lt;C-d&gt;

&lt;leader&gt;i populates the command-line with

:Import | &lt;--- cursor

and shows a list of possible completions.

Even better:

set wildcharm=&lt;C-z&gt;
nnoremap &lt;leader&gt;i :Import &lt;C-z&gt;
nnoremap &lt;leader&gt;d :Drop &lt;C-z&gt;

Supposing you have wildmenu already setup, &lt;leader&gt;i populates the command-line with

:Import firstpackagenameinthelist

with the wildmenu open and ready for &lt;tab&gt;bing.

huangapple
  • 本文由 发表于 2013年9月21日 18:13:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/18931398.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定