Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you substitute or delete between commas (or any arbitrary character) in vi/vim?

Tags:

vim

For example, I have text like this:

I talked to a friend, I hiked a mountain, I am working with blah blah, 

And I want to delete or substitute I hiked a mountain. Why doesn't ci, or di, work? Is there a way to grab, substitute, or delete between commas?

like image 950
modulitos Avatar asked Sep 15 '14 20:09

modulitos


4 Answers

there are plugins to let you create customized text object. without installing plugins, assume your cursor is between two commas, you could do:

T,c,

or

t,c,

to simulate your ci, for di, change the above c into d

related help doc:

:h t
:h T
:h ,
like image 84
Kent Avatar answered Nov 02 '22 21:11

Kent


There are a bunch of plugins that help you create custom text-objects like textobjectify.

I've had the following snippet in my vimrc for quite a while and it never failed me:

for char in [ '_', '.', ':', ',', ';', '<bar>', '/', '<bslash>', '*', '+', '%' ]
  execute 'xnoremap i' . char . ' :<C-u>normal! T' . char . 'vt' . char . '<CR>'
  execute 'onoremap i' . char . ' :normal vi' . char . '<CR>'
  execute 'xnoremap a' . char . ' :<C-u>normal! F' . char . 'vf' . char . '<CR>'
  execute 'onoremap a' . char . ' :normal va' . char . '<CR>'
endfor

It creates all the custom operator-pending mappings required for di,, vi%, ci:, ya| and so on.

And it's easy to add new delimiters.

And I have cute smileys in my vimrc.

like image 25
romainl Avatar answered Nov 02 '22 22:11

romainl


If you're willing to patch Vim for this feature, there was a proposal a while back (including a mostly working patch) to allow you to match arbitrary characters. In your example it would be cim, for "change inside matched comma". See https://groups.google.com/d/topic/vim_dev/pZxLAAXxk0M/discussion for the discussion and a few versions of the patch. I've been using it a long time and the only problem I've encountered is in visual mode; but then again, I don't use the feature as often as I expected to.

like image 1
Ben Avatar answered Nov 02 '22 20:11

Ben


If you are in a section like that you could use T,ct,.

Or you if you want to install a plugin like vim-argumentative allows you to use ci, and di,

like image 3
FDinoff Avatar answered Nov 02 '22 21:11

FDinoff