Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: select inside dots

Tags:

vim

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()
like image 678
Placoplatr Avatar asked Sep 03 '11 08:09

Placoplatr


2 Answers

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

like image 68
sehe Avatar answered Oct 11 '22 06:10

sehe


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.
like image 27
McArthey Avatar answered Oct 11 '22 06:10

McArthey