Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to apply s command to an object?

Tags:

vim

vi

For example, I often use the substitute command on all the lines in a block or a paragraph. I have an idiom that works for me:

   !ip<bs>s/foo/bar/

but every time I do it that way, I get this feeling that I do not grok the language of VI. I can do other operations on objects, like

   dip  or  yip  or cip

but not sip. It does not fit into the vi/vim language as I understand it. So, I do !ip, then erase the '!', then proceed to type in my s/// command. That is the shortest, least unnatural way of expressing that operation that I know of. Am I missing something?

like image 238
Guy Shaw Avatar asked Dec 22 '25 07:12

Guy Shaw


1 Answers

I understand your point, but I believe there is no better way using the default commands. There is an alternative when you have the +visual feature available:

vip:s/foo/bar/

But it has the same key count and no advantage. If you are open to customization, you could create a new command to do what you are expecting. For example, you could type sip and have the cursor at the command line with the selected range and s/, as described in :help :map-operator. For example:

nnoremap s :set opfunc=SubstituteOperator<CR>g@

function! SubstituteOperator(type, ...)
   silent call feedkeys(":'[,']:s/", 'n')
endfunction
like image 88
mMontu Avatar answered Dec 24 '25 04:12

mMontu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!