11

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) :

    someobject.some-property-with-hyphens.otherproperty

How to select the middle property ?

I tried :

    vi.  (dot is for executing last command)
    viw  (don't include hyphens)
    4viw (still nop)
    vis  (select full line)


Edit : more common exemple (in javascript)
    app.object['key'].$object_with_a_dollar_sign.function()
Placoplatr
  • 480
  • 2
  • 12
  • **[Updated my answer](http://stackoverflow.com/questions/7292052/vim-select-inside-dots/7292271#7292271)** added operator mappings to do make '.' a regular text object motion (_I learn everyday :))_ – sehe Sep 05 '11 at 11:17

5 Answers5

19

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).

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Definitely better than my solution – lucapette Sep 03 '11 at 09:27
  • 1
    @lucapette: I won't say _definitely_ - it depends on what the OP uses it for... I found that changing `iskeyword` and `isident` settings does take getting used to ; but if the logic matches the file's syntax, then it should be perfect! – sehe Sep 03 '11 at 09:36
  • Yep you're right again. If the logic matches the syntax file your solution is perfect. In any case, I consider your approach to the problem more vim _oriented_ it feels like vim. And I always like when I can solve a problem with vim in the vim way. I'm sure you know what I mean. – lucapette Sep 03 '11 at 09:43
  • Nice. I think modifying this matching behavior won't collide with other case where hyphens should be avoided. I will give it a try. Thanks. – Placoplatr Sep 03 '11 at 09:58
  • I will also keep "vt." in mind in case of other special characters found inside properties. – Placoplatr Sep 03 '11 at 10:01
  • Just found one. In javascript you can commonly do `app.object['key'].property`. In that case both solution are useful depending if I want to only select `object` or `object['key']` :) – Placoplatr Sep 03 '11 at 10:17
  • @Placoplatr: exactly. However, in such instances you'd be better served with a syntax-aware operator function (see the link). Chances are that this has been done by someone somewhere :) – sehe Sep 03 '11 at 10:27
  • **Updated** added operator mappings to do make '.' a regular text object motion – sehe Sep 05 '11 at 11:11
1

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.

sehe
  • 374,641
  • 47
  • 450
  • 633
lucapette
  • 20,564
  • 6
  • 65
  • 59
  • 1
    It does the trick. BUT it will also select intervening spaces, parentheses etc. An example, in: `Console.WriteLine(new Random().Next));`it will select `WriteLine(new Random()` in one fell swoop. Probably not what you intended. – sehe Sep 03 '11 at 09:27
  • Yes you're right. I was focused on the examples shown. By the way, I tend to forget about iskeyword option. – lucapette Sep 03 '11 at 09:32
1

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.
McArthey
  • 1,614
  • 30
  • 62
0

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 ' ".

Omid
  • 160
  • 2
  • 9
0

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.

arunmur
  • 650
  • 4
  • 5