vim: select inside dots

后端 未结 5 2097
孤独总比滥情好
孤独总比滥情好 2020-12-16 17:10

I can\'t find a solution (here and on the web) for simply selecting/inserting/deleting stuff surrounded by dots (a common case in development) :

    someobje         


        
相关标签:
5条回答
  • 2020-12-16 17:48

    Maybe this is not what you're looking for, but I used the standard search functionality and typed in

    /\..*\.
    and it selected the .some-property-with-hyphens. value in your example above.

    0 讨论(0)
  • 2020-12-16 17:51

    I suspect the real issue here is that hyphens are not considered a part of an identifier

    You should try adding

    :se iskeyword+=-
    

    for your file type. That way, viw will do exeactly what you want

    To make this setting automatic for, say, strange files:

    :autocmd BufReadPost *.strange se isk+=-
    

    Adding that line to your vimrc (:e $MYVIMRC) you'll never have to think about adding the iskeyword setting. See also :he modeline for alternative ways to set this setting per file


    Update an even purer solution would to create your own operator-mapping.

    A quick draft of this, that seemed to work very nicely for me:

    xnoremap <silent>.  f.oT.o
    xnoremap <silent>a. f.oF.o
    xnoremap <silent>i. t.oT.o
    
    onoremap <silent>.  :<C-u>exec 'normal v' . v:count1 . '.'<CR>
    onoremap <silent>a. :<C-u>exec 'normal v' . v:count1 . 'a.'<CR>
    onoremap <silent>i. :<C-u>exec 'normal v' . v:count1 . 'i.'<CR>
    

    Examples for the following buffer contents (cursor on the letter w):

    someobject.some-property-with-hyphens.SUB.otherproperty
    
    • v. selects some-property-with-hyphens. in visual mode
    • va. selects .some-property-with-hyphens. in visual mode
    • vi. selects some-property-with-hyphens in visual mode

    Motions can be chained and accept a count:

    • v.. selects some-property-with-hyphens.SUB. in visual mode
    • v2. also selects some-property-with-hyphens.SUB. in visual mode
    • v2a. selects .some-property-with-hyphens.SUB. in visual mode
    • v2i. selects some-property-with-hyphens.SUB in visual mode

    You can use the operators as operators to any editing command:

    • d. results in someobject.SUB.otherproperty
    • ci.shortname results in someobject.shortname.SUB.otherproperty
    • c2.get(" results in someobject.get("otherproperty

    It doesn't matter where in a 'dot-delimited-identifier' the cursor is to start with. Note that for convenience, all visual mode mappings position the cursor at the end of the selection (so you can do continue extending the selection by e.g. % and other motions).

    0 讨论(0)
  • 2020-12-16 17:58

    I'm not absolutely sure (I learn new vim features everyday) but I think you can't select between dots with text-objects. So, if it's a common case for you maybe you can create a mapping like the following:

    nnoremap <leader>d t.vT.
    

    Just to avoid typing five characters each time you need to select between dots.

    0 讨论(0)
  • 2020-12-16 17:59

    You might want to try this although it's not a proper answer:

    put you cursor on s (after dot) and type: (which means visual until dot).

    vf. 
    

    I believe the reason that you can't use vi. is dot is not something that vim could find its another pair like ( or ' ".

    0 讨论(0)
  • 2020-12-16 18:03

    I dont know any generic solutions. For select/delete/insert I use regular experssions.

    :%s/\.some-property-with-hyphens\./.i-like-this-property./g
    

    To just select

    /\.some-property-with-hypehns\.
    

    A more generic rule

    /\.[\-a-zA-Z0-9]*\.
    

    Hope this is what you are asking.

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