# Applying an ex command to a visual selection or text object

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.

• 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 '15 at 0:35
• 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 '15 at 4:16
• 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 – Christian Brabandt Oct 11 '15 at 7:44