1

This problem started because I want to run a command in visual mode with

:silent! '<,'> command here

however if I do a mapping

vnoremap ,a :silent! '<,'> command here

then in vim runs like

:'<,'>silent! '<,'> command here

the same happens if i use

vnoremap ,a :exe "silent! '<,'> command"

that it is converted to

:'<,'>exe "silent!...."
  • 1
    On a side note, prefer to exclude the select-mode -> :xnoremap <silent> ,a :<c-u>silent! exe.... – Luc Hermitte Apr 26 '18 at 11:48
  • interesting point. I will have a look. Thank you – manolius Apr 27 '18 at 11:46
7

Putting aside mappings for a moment, what do we do when we're manually entering commands and find a range on the command line that we don't want? Hit Backspace five times? Well...yes...that's one possibility but there's something better than that:

                                                    c_CTRL-U
CTRL-U      Remove all characters between the cursor position and
            the beginning of the line. ...

Since a mapping is essentially the same as the equivalent manual command we just need to translate Ctrl+U to something that works in Vimscript. That would be the equivalent keycode <C-U>.

So add <C-U> at the beginning of your mapping right after the opening :. For example:

vnoremap ,a :<C-U>silent! ..etc..

That will clear any text that happens to currently be on the command line before your command(s) are applied.

| improve this answer | |
  • 1
    I didn't remember the existence of <C-U>. Brilliant – manolius Apr 27 '18 at 11:47

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.