如何在 Vim 中为命令创建别名?
Vim 是我编程时首选的文本编辑器,因此我总是遇到一个特别烦人的问题。
通常,当我快速需要保存缓冲区并继续执行其他杂项任务时,我会执行典型的操作。
:w
但是,我总是 ——似乎超过 50% 的时间 ——设法利用这一点 :w
。自然地,Vim 对我大喊大叫,因为 W
是一个无效命令:
E492: Not an editor command: W
我的问题是如何在 Vim 中为冒号命令起别名。特别是,您能否举例说明如何将 W
别名为 w
。
我知道将按键映射到某些命令的过程,但这不是我想要的。
Vim is my preferred text editor when I program, and thus I always run into a particularly annoying issue.
Frequently, when I quickly need to save the buffer and continue on to some other miscellaneous task, I do the typical
:w
However, I always — what seems to be like more than 50% of the time — manage to capitalize that :w
. Naturally, Vim yells at me because W
is an invalid command:
E492: Not an editor command: W
My question is how can one alias colon-commands in Vim. Particularly, could you exemplify how to alias W
to w
.
I am aware of the process to map keys to certain commands, but that is not what I’m looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
最安全和最简单的是插件,例如 cmdalias.vim 或我最近的更新它的 vim-alias 考虑了
:sil(ent)(!)
或:redi(r)
,'<,'>
视觉选择,Safest and easiest is a plugin such as cmdalias.vim or my recent update vim-alias of it that take into account
:sil(ent)(!)
or:redi(r)
,'<,'>
for the current visual selection,我认为@ZyX的答案很好,但是如果您使用的是较新版本的neovim(0.5+),您可能想使用lua来定义函数。这是一种方法:
然后,您可以从
call SetupCommandAlias("pg", "postgres://")
中删除call
并仅使用该函数像这样:SetupCommandAlias("pg", "postgres://")
。注意:如果从
.vim
文件而不是.lua
文件使用它,则需要在函数调用前加上lua
前缀,即lua SetupCommandAlias("pg", "postgres://")
。I think @ZyX's answer is great, but if you're using a newer version of neovim (0.5+), you might want to define the function using lua instead. Here's one way you could do it:
Then, you'd drop the
call
fromcall SetupCommandAlias("pg", "postgres://")
and just use the function like this:SetupCommandAlias("pg", "postgres://")
.n.b. If using it from a
.vim
file instead of a.lua
file, you'd need to prefix the function call withlua
, i.e.lua SetupCommandAlias("pg", "postgres://")
.也许您想将功能键之一 (F1..F12) 映射到 :w ?然后将其放入您的 .vimrc 中:(
插入模式下的 ctrl-o 暂时切换到正常模式)。
Maybe you would like to map one of your function keys (F1..F12) to :w ? Then put this into your .vimrc:
(ctrl-o in insert mode switches temporarily to normal mode).
最好的解决方案包括 编写自定义函数 用于处理仅出现在命令栏开头的缩写。
为此,请在 vimrc 文件或其他任何位置添加以下内容。
一些源材料中的有用缩写我在哪里找到这个东西:
The best solution involves writing a custom function for handling abbreviations that only take place in the beginning of the command bar.
For this, add the following your vimrc file or anywhere else.
A few useful abbreviations from the source material where I found this stuff:
假设你想在 gvim 中为 tabnew 命令添加别名。您只需在 .vimrc 文件中键入以下命令(如果不在主文件夹中,则创建一个)
Suppose you want to add alias for tabnew command in gvim. you can simply type the following command in your .vimrc file (if not in home folder than create one)
我发现将
;
键映射到:
将是一个更好的解决方案,并且可以提高您键入其他命令的效率。I find that mapping the
;
key to:
would be a better solution, and would make you more productive for typing other commands.要保持完成不变,请尝试使用
它将命令行中的
W
替换为w
,但前提是它后面或前面都没有单词字符,所以:W
将替换为:w
,但:Write
不会。 (请注意,这会影响任何匹配的命令,包括您可能不期望的命令。例如,命令:saveas W Z
将替换为:saveas w Z
,因此请小心这一点。)更新
这是我现在的编写方式:
作为函数:
这会检查命令类型是否为
:
并且命令是否为W
,所以它比cnoreabbrev W w
更安全。To leave completion untouched, try using
It will replace
W
in command line withw
, but only if it is neither followed nor preceded by word character, so:W<CR>
will be replaced with:w<CR>
, but:Write
won’t. (Note that this affects any commands that match, including ones that you might not expect. For example, the command:saveas W Z
will be replaced by:saveas w Z
, so be careful with this.)Update
Here is how I would write it now:
As a function:
This checks that the command type is
:
and the command isW
, so it’s safer than justcnoreabbrev W w
.通过补充搜索,我发现有人问了几乎相同的问题 正如我
会做的那样。
请注意,正如 Richo 指出的那样,用户命令必须以大写字母开头。 >
With supplementary searching, I've found that someone asked nearly the same question as I.
will do the trick.
Please be aware that, as Richo points out, the user command must begin with a capital letter.