0

I have the following dysfunctional mapping

" Capitalise each word in selection
vmap gw :'<,'>s/\<./\u&/g<CR>

which should do what the comment says. But when I use it, vim reports E488: Trailing characters. It works when I leave out the selection marks '<,'>, but then it applies to the whole line.

I suspect I need to escape something here, but the help page only lists some other characters which must be escaped.

  • 1
    I think you can also capitalise from visual mode with the mapping gU. – Karl Yngve Lervåg Oct 25 '15 at 16:30
  • @KarlYngveLervåg I do not want to capitalise each character, merely the first one in each word. – oarfish Oct 25 '15 at 16:32
2

When you enter command-line mode from visual mode Vim automatically populates the command-line with '<,'>. Your mapping adds another '<,'> after the automatic one which makes your command invalid.

This:

xnoremap gw :s/\<./\u&/g<CR>

does exactly what you'd expect.

--- edit ---

Substitutions are line-wise by default. If you want to restrict your substitution to the current visual selection you need to add the \%V atom to your search pattern:

xnoremap gw :s/\%V\<./\u&/g<CR>

See :help \%V.

  • For me, it capitalises each word in the line my selection is on. So, everything inside the selection is processed, but also stuff before and after. – oarfish Oct 25 '15 at 16:33
  • Yes of course. What did you expect? Substitutions are line-wise by default. See my edit. – romainl Oct 25 '15 at 18:07
  • I did not realise that. Thanks for clearing that up. – oarfish Oct 25 '15 at 23:10

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.