Oscar Godson
Oscar Godson

Reputation: 32726

Why isn't this Vim mapping working?

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.

Upvotes: 0

Views: 183

Answers (4)

Peter Rincker
Peter Rincker

Reputation: 45117

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

Upvotes: 0

XZS
XZS

Reputation: 2494

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.

Upvotes: 1

Gary Fixler
Gary Fixler

Reputation: 6028

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.

Upvotes: 3

Kent
Kent

Reputation: 195059

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.

Upvotes: 0

Related Questions