1

I can make a map like this:

nmap wr :write<CR>

but then it clashes with another map:

So if I do press w, it pauses before performing the motion. How can I quickly perform the w motion? I tried ww, but then it motions twice.

2 Answers 2

2

In order to trigger the mapping before 'timeoutlen' expires you can hit any other key that will not have a side effect and work as a null operation.

In this case, in Normal mode, you can use <Esc>, which does nothing if you're already in Normal mode. So you could use w<Esc> to have the w motion applied right away.

(Of course, the best solution is to avoid such mappings in first place, mappings such as wr which shadow useful commands such as the w motion. Prefer to put your mappings under a <Leader> prefix to have them in a separate namespace outside of the regular Vim commands.)

0

Another option would be to use capital instead:

nmap W :write<CR>

This will likely clobber something else (as in this case), but usually the capitals are less used than lowercase. Another option is abbreviate:

http://vimdoc.sourceforge.net/htmldoc/map.html#:abbreviate

Example:

abbrev w write

This avoids the ambiguous map, as the example above only works in command mode. Drawback is that is take more keys, as its Shift : w Enter, instead of just w r. Also in this case its redundant as you can already use w instead of write. However I found this useful for other cases.

1
  • 1
    Problem with a simple abbreviation like that is that it's expanded in way more contexts than you expect... You can use :cabbrev to make it only work in command-line (and search, and input...) But it's still a problem, for instance try opening or saving a file named w.txt with that abbreviation... You can work around that, but you typically need an <expr> mapping checking you're in command-line with getcmdtype() and use getcmdpos() to check it's the first word in the line (only expand as the "command", not in arguments.)
    – filbranden
    May 13, 2020 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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