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

后端 未结 4 877
日久生厌
日久生厌 2021-02-04 16:21

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

相关标签:
4条回答
  • 2021-02-04 16:52

    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 ,
    
    0 讨论(0)
  • 2021-02-04 16:55

    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,

    0 讨论(0)
  • 2021-02-04 17:03

    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.

    0 讨论(0)
  • 2021-02-04 17:13

    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.

    0 讨论(0)
提交回复
热议问题