0
noremap :hsp :botright new
noremap :vsp :botright vnew

"Not an editor command: hsp"

I'm probably googling the wrong thing, but I can't find many results on aliasing vim commands. I can find tons of info about mapping keys to commands like my one for tabs:

noremap <C-t> :tabnew<CR> 

But can't find commands mapped to other commands.

Oscar Godson
  • 31,662
  • 41
  • 121
  • 201

4 Answers4

3

What you're doing is simulating a command with a mapping. You're saying that when you press the 4 keys :hsv in normal mode, it should type out the keys :botright new (which would need a <CR> to run, as others have said), but it's not actually making the command hsv. You could make an actual command with a user command (:h user-commands). These must start with a capital letter, though.

:command Hsp botright new
:command Vsp botright vnew

Now you can type :Hsp and hit enter to run the command botright new.

Gary Fixler
  • 5,632
  • 2
  • 23
  • 39
1

Did you try command abbreviation?

ca hsp botright new
ca vsp botright vnew

You will have to initialize the expansion of the abbreviation by hitting the space key afterwards. Depending on the global vim configuration, expansion also happens automatically just when enter is pressed.

XZS
  • 2,374
  • 2
  • 19
  • 38
  • That did it thanks! My google-fu must just suck :( I kept getting things about shortcuts. – Oscar Godson Sep 04 '13 at 20:51
  • P.S. you answered in <10 minutes so I have to wait 10 minutes before marking yours correct. – Oscar Godson Sep 04 '13 at 20:51
  • 2
    this is not `alias`, it is abbreviation. you need a key (e.g. space) after the `hsp` to trigger the ab. I suggest command mapping. – Kent Sep 04 '13 at 21:17
  • Kent is right. Command abbreviations are dangerous! These abbreviations will expand in the middle of a command, in search, etc. May want to look here: http://stackoverflow.com/questions/7513380/vim-change-x-function-to-delete-buffer-instead-of-save-quit/7515418 – Peter Rincker Sep 04 '13 at 22:29
0

with your same mapping, I cannot get the Not an editor command: hsp error message with my vim (v7.4).

Your mapping works fine, but you don't have <cr> at the end, so when you press :hsp in normal mode, your mapping will switch to commandline mode, and put the mapped command there, without executing it. You have to manually press Enter.

@XZS's answer works, but keep in mind that it is an abbreviation(ab), not a mapping. ab is not command aliases, it is not exactly same as mapping. For example, you have to press another key (like space) after hsp to trigger the ab. also, you cannot ab some special keys, this would be another limitation of ab.

There is c(nore)map for command mapping.

e.g. you could have:

cnoremap hsp botright new

with above line, as same as your original one, you have to manually press Enter, if you want it to be executed, you need add <CR> at the end of the line.

I think if I do this, I would create mapping.

Kent
  • 189,393
  • 32
  • 233
  • 301
  • This suffers the same problem as the `cnoreabbrev`. It will expand in unexpected places, e.g. searching. It would be better to create a command or use a special expression `cnoreabbrev`. See http://stackoverflow.com/questions/7513380/vim-change-x-function-to-delete-buffer-instead-of-save-quit/7515418 – Peter Rincker Sep 04 '13 at 22:33
  • @PeterRincker thank you! you are right, the mapping will be triggered at `/ or ?` too...not nice. The `command` would be better. answer pending to be deleted. – Kent Sep 05 '13 at 09:18
0

Creating command alias can be tricky:

  • Using a simple cabbrev and/or cmap will cause expansions and mappings to fire in unexpected places like during searches with / and in the middle of filenames.
  • cmap's will have a visible delay in outputting to the screen which is why cabbrev is often used.

However there are a few ways to create a proper alias:

  • Create a command via :command.
    • e.g. command W w
    • command's first letter must be a uppercase letter
    • must supply -nargs, -bar, -complete, and -range options according to the needs of your alias
  • Expression :cabbrev to guard the abbreviation from expanding in in proper places.
    • expression mapping use the <expr> option
    • verify getcmdtype() is equal to :
    • verify the abbreviation is at the beginning of command line via getcmdline() or getcmdpos()
    • e.g. cnoreabbrev <expr> W getcmdtype() == ':' && getcmdline() ==# 'W' ? 'w' : 'W'
  • Use :Alias via the cmdalias.vim plugin by Hari Krishna Dara
    • e.g. Alias W w
    • uses an expression cabbrev under the covers similar to the technique above
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101