英文:
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
,等等...我可以定义这些命令吗?
我偶然发现了这个问题,但我不明白它在做什么,也不确定答案是否适用于这个问题。我可以通过将链接帖子中的所有w
和W
替换为i
或d
来解决这个问题吗?
另外,这与不带变量的情况有什么不同(比如,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,最好使用cnoreabbrev
或cnorea
,如此答案所指出。
更好的工作示例(粘贴到~/.vimrc
中):
cnorea <expr> i ((getcmdtype() is# ':' && getcmdline() is# 'i')?('Import'):('i'))
cnorea <expr> d ((getcmdtype() is# ':' && getcmdline() is# 'd')?('Drop'):('d'))
编辑:简单的答案。只需使用cabbrev
或ca
(命令缩写)似乎可以工作:
工作示例(将其粘贴到~/.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 <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
得分: 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 <leader>i :Import<Space>
nnoremap <leader>d :Drop<Space>
In normal mode, <leader>i
populates the command-line with
:Import | <--- cursor
ready for you to type in or <tab>-complete
an argument.
Better:
nnoremap <leader>i :Import <C-d>
nnoremap <leader>d :Drop <C-d>
<leader>i
populates the command-line with
:Import | <--- cursor
and shows a list of possible completions.
Even better:
set wildcharm=<C-z>
nnoremap <leader>i :Import <C-z>
nnoremap <leader>d :Drop <C-z>
Supposing you have wildmenu
already setup, <leader>i
populates the command-line with
:Import firstpackagenameinthelist
with the wildmenu open and ready for <tab>
bing.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论