0

I'll try to explain by example..

If we have a piece of code like this in vim:

if ($feck == true && $drink == false)
{
    echo 'They lie in wait like wolves..';
}

And I go to visual mode and select "$drink" for example, is there a way to:

  • detect whether the current selection is one of vim's text-objects (word, WORD, inner {, etc.)
  • do a lookup on both sides of the selection to check for the next available vim text-object (again, word, WORD, a block, inner ", etc..)

Please note that I'm thinking of vim scripting not just plain editing, so both of these "actions" would be in a function, sourced in my .vimrc

Also, I would need to do this without breaking the visual selection or moving the cursor (or, if it really can't be avoided, restore both the selection and cursor position).

5
  • Are you sure that visual selection is the right place to start? Commented May 27, 2009 at 19:52
  • This is actually related to my first question on SO, so pretty much yes :) I've looked everywhere (i.e. googled), but found nothing similar to this type of script. :-/ Commented May 27, 2009 at 19:55
  • Hello my fellow countryman. I'm trying to figure out what are you trying to accomplish. Are you sure you're taking the right approach here. Although what you are trying is theoretically possible, vim has other (and pretty good, I might add) methods of selecting selection. And even then, I can't see what purpose would that serve. Commented May 27, 2009 at 22:05
  • Vim's WORD is changeable. You can change what a word is made of (see :h iskeyword). As far as scripting goes, vimscript is made out in a way that anything you do can be made scriptable (since it is the same thing). Vimscript is more of a vim-macro. Now, the last part - as far as keeping the cursor in place goes, you can't really not break the position. You can however, mark it and then get back to it with singlequote-mark (differs from quote-mark), ma, and then 'a or `a. Commented May 27, 2009 at 22:08
  • Hey Idigas (cro?:)). My idea is to check the scope of current visual selection and then check the surrounding area for the next available "scope" of selection. In the example above, if I have $drink selected, my next probable candidates would be "i)" or "a)", after that the whole if block. The idea of course remains the same as in my previous question: to use a single shortcut for extending the current selection, whatever that selection might be ATM. Commented May 28, 2009 at 7:21

2 Answers 2

1

This question only makes sense in the context of this other question by dr Hannibal Lecter.

I think what you want to achieve is possible, but it requires some work. The information given in the other answer and the comments to your post, already point in the right direction.

Your vim function that is called from your "extend selection" key mapping must do the following:

  1. Check what the current selection probably represents (markers '< and '> as dwc describes and vim functions should be helpful for this). Questions like this must be answered: is '< and '> on the same line? Are all letters between '< and '> iskeyword characters? Are the first and last characters of the selection special delimiters like '{','(', etc. ? And so on - there is no builtin functionality that checks this automatically. See also :help visualmode(

  2. Once this is determined, you have to think, how you then would like to extend your selection further - however the logic for this works in ReSharper.

  3. You will need to take care of putting the cursor back where it was, depending on the commands you used to extend the selection.

In general, the commands presented in :help eval.txt will be helpful in implementing this.

Sign up to request clarification or add additional context in comments.

2 Comments

Excellent, exactly the tips I need to get started. Thanks.
Hey, just stumbled across this question. Did you ever get around to getting a script of this working?
0

After a visual selection is made, '< and '> are available for use in anything that accepts a "range."

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.