Even though I am a relatively precise typist, it is a common error for me to wallop the apostrophe character when I go to hit a carriage return in Vim. This happens especially when I am trying to type :w<CR> or wq<CR>. I end up typing :w'<CR>, or even worse, :wq'<CR>, which gives a couple big red error messages in addition to not doing what I intend. Is there a mapping or some other technique that can be used to overcome this? I can’t imagine why I would ever want to write a file named ', so I am not concerned about blocking that possibility entirely.
Even though I am a relatively precise typist, it is a common error for
Share
You must be careful with using command mappings (and abbreviations), they often fire when they are not supposed to for example during a search or in the middle of a command argument. I typically prefer to use a expression abbreviation.
Put the following in your
~/.vimrc:This abbreviation uses a function called
EatCharwhich takes a pattern that will be “eaten” or ignored after the abbreviation is expanded. See:helpg Eatcharfor an different example of its usage.The abbreviations will only expand and look to “eat” a quote when all the following criteria is met:
getcmdtype() == ':'getcmdline() ==# 'w'Otherwise the abbreviation is expanded to the same as the
{lhs}i.e.worwqDisadvantages to this approach:
:w'you will have to type the quote twice , use a space to separate the command form the filename, or type Ctrl+v followed by 'For more information see: