Is it possible to apply an ex command to something smaller than a line? For instance, I tried to define a mapping that would remove an \emph{} from the enclosed word. The best I could do was:

nnoremap dsi vaW:s/\\emph{//\|s/}//<cr>

It turns out that this does not operate on the selection but rather on the line.

PS. I am satisfied with the comments to the effect that :s can operate on selections if \%V is placed at the beginning of the pattern.

  • 3
    How about noremap dsi vaW:s/\%V\\emph{//\|s/}//<cr> ? See :h /\%V. And, unless you have a special need, always use noremap, not nmap, if you want to avoid surprises. Also your map won't like spaces (more than one emphasized/enclosed word).
    – VanLaser
    Oct 11, 2015 at 0:35
  • 1
    Not that I know of. If you had surround installed you could do something like nmap dsf ds{dF\ (I mean nmap since I want ds{ to call the surround mapping.)
    – FDinoff
    Oct 11, 2015 at 4:16
  • 4
    not really. Ex commands always refer to complete lines. You can work around it by yanking your visual selected range, use some functions to work with the yanked text and replace the yank back. Alternative solutions include using Dr.Chips vis.vim plugin or using the \%V atom specifically for the :s command Oct 11, 2015 at 7:44

1 Answer 1


Perhaps http://www.drchip.org/astronaut/vim/index.html#VIS 's :B command will do what you want -- it applies the command to the visual block selection.

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.