20

How can I map :E to :Explore? I've installed an extension that leads to E464: Ambiguous use of user-defined command if I do :E now, but my fingers won't forget the command!

I tried map :E :Explore, but that's ugly since it makes accessing the other commands difficult.

I tried these:

cmap :E<CR> :Explore<CR>
cmap :E^M :Explore^M

(where ^M = control-v + enter) but these don't work unless I hit enter really really fast.

keflavich
  • 18,278
  • 20
  • 86
  • 118

1 Answers1

36

:E would normally suffice as is if :Explore were the only defined command that began with an E. You evidently have multiple such commands defined, so :E is ambiguous and results in an error.

:cmap causes immediate literal substitution and thus has unwanted side effects. A slightly better alternative is :cabbrev, which can be used to define abbreviations for command mode:

cabbrev E Explore

This triggers following EEnter or ESpace. The former is desired because typing :EEnter will invoke :Explore, but the latter again has side effects in command mode.

In order for :E to be properly aliased to :Explore, it must be defined as a separate command:

command! E Explore

However, :command E, which lists all defined commands that start with E, reveals that :E and :Explore have different properties. For example, it's impossible to execute :E ~ because :E does not accept any arguments. Also, unlike :Explore, :E does not autocomplete directories.

To remedy these deficiencies, :E must be defined in exactly the same way as :Explore. Executing :verbose command Explore shows the location of the script in which :Explore is defined; :E can then be defined in the same manner, with the addition of <args>:

command! -nargs=* -bar -bang -count=0 -complete=dir E Explore <args>

While it's possible to deduce most of these attributes from the information provided by :command Explore, there can still be discrepancies, such as -bar in this case.

N.B. If :Explore and :Example are defined, :Exp and :Exa are the shortest unambiguous commands that can be used. Explicitly aliasing :E to one of them, as above, overrides Vim's default behavior and allows for disambiguation. However, :Ex would still be ambiguous.

Nikita Kouevda
  • 5,508
  • 3
  • 27
  • 43
  • 11
    Another quick way to open up the explorer is using `:e.`. This will `:Explore` the current directory. This also has the nice benefit of not needing the shift key. – Peter Rincker Jan 17 '13 at 18:39
  • @PeterRincker Neat, but for a totally different use case. – Shriken Jul 15 '15 at 21:05
  • 1
    For the complete n00b, put `command! E Explore` in your `~/.vimrc` file, then :E will properly alias to :Explore. – N0thing Dec 07 '16 at 07:25