sidyll
sidyll

Reputation: 59317

Entering command correctly in visual mode

I need to do a map for visual mode to apply some commands on the selection only. This implies that the '<,'> needs to appear in the command line.

As they appear automatically when you type :, I did the mappings without delimiters, something like vmap ,{key} :{command}. So far it's working, they are being shown as :'<,'>{command}.

But the question is, is it safe to rely on this behaviour? Or there is a better way to do a visual map and insert the delimiters?

Upvotes: 1

Views: 209

Answers (2)

bobbogo
bobbogo

Reputation: 15523

You could use <c-u> at the start of your mapping to clear any range that might be there, and then add the range markers explicitly (:<c-u>'<,'>).

  • :help omap-info
  • :help c_CTRL-U

Upvotes: 3

jamessan
jamessan

Reputation: 42757

Yes, it's safe to rely on that behavior. Vim always insert the visual range markers ('< and '>) when you press : while in visual mode. Since a map is just a way to store a series of key presses (ignoring <expr> maps), it acts just like you had typed it.

Upvotes: 4

Related Questions