Vim Visual Select Around Variables

I am wondering if there is a way to selectively select variables in the same way as you can select blocks using commands like va} . For example, to distinguish between php and ruby, there is some analysis of the language, for example. For future reference, it would be nice to use this - ideally select various syntax elements.

For instance. I would like to select around $array['testing'] in the following php line:

 $array['testing'] = 'whatever' 

Or, say, I want to select a list of block parameters |item, index| here:

 hash.each_with_index { |item, index| print item } 

EDIT:

Specific regular expressions may address different issues individually, but I believe there should be a way to use parsing to get something much more reliable here.

+4
source share
2 answers

Although your examples above are quickly selectable with Vim embedded text objects (the first is just viW , for the second I would use F|v, ), I admit that Vim syntax highlighting can be a good source for movements and text objects.

I saw the first implementation of this idea in the SyntaxMotion plugin , and recently I implemented a similar plugin: SameSyntaxMotion . The first defines movements for the normal and visual mode, but does not contain pending and text objects. It does not skip the contained sub-syntax elements and uses the same color as the difference property, while mine uses syntax (which can be more precise, but even more difficult to understand) and has text objects ( ay and iy ) too.

+3
source

You can define your own text objects in Vim.

The easiest way to create custom text objects is to define :vmap (or :xmap ) for the visual mode part and :omap for the operator standby part. For example, the following displays

 xnoremap aC F:o, onoremap aC :normal! F:v,<CR> 

allows you to select a bit of text enclosed in a colon. Try vaP or daP above the word "colon" below:

 Some text :in-colon-text: more of the same. 

See :h omap-info for another short example :omap .


If you don't mind depending on the plugin , however, textobj-user . This is a universal framework for custom text objects written by Kana Natsuno. There are already great text objects for this structure, such as textobj-indent , which I find indispensable.

Using this, you can easily implement objects that depend on the file type for variables. And make it accessible to everyone!

+2
source

All Articles