vi/vim alternative to sublimetext's "Expand selection to scope"

Sublime text has a cool feature called "Expand selection to scope" SHIFT+CMD+SPACE which selects the everything within the scope.

It select everything in scope, works perfectly ( ) or [ ] or { } repeating it expands the scope to its parent.

I'm looking to achieve the same with vim.


Solution 1:

If you meant vim you can do this with visual-mode (:help visual-mode) and text-objects (:help text-objects).

To select a curly-braced block do: v+a+{, to select the enclosing block repeat a+{. Note that you can choose to only select the contents of the braces by using i instead of a.

These commands, as many others in vim, are built up by an action followed by a text-object, where the text-object can be prepended by a number to include more objects affected by the action. So you could also delete the object by replacing v by d or correct it with c, etc.

There are text objects for a lot of other things besides (), {} and [], e.g.:

  • a+w means a word.
  • a+s means a sentence.
  • a+p means a paragraph.
  • a+< means a <> block.
  • a+' means a single-quoted string.
  • a+" means a double-quoted string.

You may also be interested in the surround plugin which allows you to add/replace/delete surrounding characters or even tags.

Solution 2:

The vim-expand-region plugin allows to extend / shrink the visually selected region to a (configurable) set of text objects. I.e. you can start with selecting a variable, then assignment, then block, then function, etc.