16

In vim, in my .vimrc, how can I redefine a command (i.e. :e) as something else? I want to redefine :e * as :tabe *.

Ciro Santilli OurBigBook.com
391k118 gold badges1.3k silver badges1.1k bronze badges
asked Apr 9, 2010 at 4:11

3 Answers 3

14

cnoreabbrev workaround

The best workaround without a plugin that I have seen so far is:

cnoreabbrev <expr> e getcmdtype() == ":" && getcmdline() == 'e' ? 'tabe' : 'e'

If you use a simple:

cnoreabbrev e E

as suggested by @Yktula would break your command if you want to do:

echo e f

which would become:

echo E f

instead.


As explained by @pydave in the comments, the cmdalias plugin adds a simpler interface to doing the safe alias simply as:

:Alias e tabe

At the time of writing the plugin also uses the getcmdtype() == ":" technique to implement its functionality.

answered Dec 13, 2013 at 10:48

1 Comment

You can use cmdalias to simplify that to :Alias e tabe and it will do the getcmdline magic for you.
12

I figured out a way to do it. See How to disable a built-in command in vim . From that, we can see that we can use cabbrev to change what a command does. For my needs, cabbrev e tabe is perfect.

But we can generalize this solution to make commands starting with lower case characters accessible to users for user-defined ones: use cabbrev to (re)define a built-in command as a user-defined one. As such, we are able to redefine built-in commands as well as user-defined ones.

Here's an example, which is equivalent to my aforementioned solution to my problem:

:command -nargs=+ E :tabe "<args>"
:cabbrev e E

That's all.

answered Apr 9, 2010 at 5:02

1 Comment

This breaks if you want to type: echo e f, which would become echo E f instead. My answer attempts to overcome this.
2

As I understand it, you can't. User defined commands must have an uppercase first letter.

 :help :command

for more information

answered Apr 9, 2010 at 4:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.