0

I know that this is probably better to solve by not typing sloppily in the first place, but I just think it would be really nice to have.

Since I've used Vim for a while, the ;w (I have mapped normal mode ; to :) is a bit of a reflex for me. However, what I dont like is that sometimes i type fast and that causes bad things to happen.

Take a look at my vimrc...

"""" Commenting out cmaps because cmap applies when e.g. typing in (/)-search mode
" " prevent common typos from actually writing files to disk
" cmap wq1 wq!
" cmap w1 w!
" cmap q1 q!
" cmap qa1 qa!
" cmap e1 e!
"
" cmap w; w
"
" cmap qw wq
" cmap qw! wq!
" cmap qw1 wq!
"
" " and more combinatorially exploding goodness for dealing with flubbing the
" " enter key
" cmap wq\ wq
" cmap wq1\ wq!
" cmap wq!\ wq!
" cmap w\ w
" cmap w1\ w!
" cmap w!\ w!

This goes on for 50 or so more lines.

They're all commented because cmap applies when using / to search. That's a dealbreaker.

And this doesnt even cover the cases where I write :w and then happen to mash some special char after it, and this somehow causes vim to write the buffer into a file named after that character. Great way to lose some work.

Any ideas? This is gonna require me to edit vim source code isn't it...

  • 1
    Sloppy typing is the issue, here: don't look for workarounds, fix it. – romainl Dec 9 '14 at 9:22
2

You're right, the downside of :cmap is that it applies to all command-line modes, so it prevents you from searching for those strings, too.

One solution is to check the type of command-line mode, via an expression mapping:

cnoremap <expr> w1 getcmdtype() == ':' ? 'w!' : 'w1'

If you're clever, you can do meta-programming and define all your mappings in a loop, just passing in the source and target strings.

Even those modified mappings still don't allow you to type those mapped strings, e.g. as command arguments (so you can't easily :w w1, only by escaping the latter via <C-V>), though.

Plugin

The cmdalias.vim - Create aliases for Vim commands plugin also checks the command position and therefore handles arguments. It also offers a convenient :Alias command, so you don't need to do metaprogramming.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.