user137369
user137369

Reputation: 5686

How to map (alias?) a command in vim

Lets say, hypothetically, that I always forget the command :vsplit, and always think it’s :vdivide.

Is there a way to map (I’m not sure if that’d be the right thing to call it, since it’s a command) one to the other?

Upvotes: 10

Views: 10203

Answers (3)

Jay Taylor
Jay Taylor

Reputation: 13562

2024 Vim Aliasing Update:

After extensive testing and research, I learned the following is the most resilient way to alias a command in Vim. I wanted to alias :Set to :set, because it's a frequent typo when I'm trying to quickly do :set paste.

Many of the alias techniques on StackOverflow and the internet at large do not take into account handling aliased command arguments, but this solution works perfectly or can be trivially tweaked for every case I've encountered.

Add this to your .vimrc file:

" Aliasing: This is a robust technique
" n.b. For further details about nargs behavior, see: `:h command'
command -nargs=* Set set <args>

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172540

To get around the limitation that user-defined commands must start with an uppercase letter, I recommend the cmdalias.vim - Create aliases for Vim commands plugin. With it, you can do:

:Alias vdivide vsplit

Upvotes: 7

Xavier T.
Xavier T.

Reputation: 42218

User defined command can only start with an uppercase letter, so you can only add :Vdivide but not :vdivide.

Add in your .vimrc

 command Vdivide vsplit

Upvotes: 12

Related Questions