18

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.

palaniraja
  • 341
  • 2
  • 10

3 Answers3

27

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.

Thor
  • 6,419
  • 1
  • 36
  • 42
  • Yes, I was referring vim. Thank you. is there a way to keep expanding it eg., `[[[event touchesForView: button] anyObject]` if cursor is near `touchesForView` action `v`+`a`+`[` selects `[event touchesForView: button]` I would like to expand one more level? i.e., `[[[event touchesForView: button] anyObject]` – palaniraja Apr 22 '13 at 13:27
  • 4
    @palaniraja: yes there is, you can prepend the text-object by a number, e.g.: `v`+`2`+`a`+`[`. – Thor Apr 22 '13 at 13:32
  • Is there an easy way to shrink the visual selection back to the previous one? E.g. if I did `v` and then `a)` thrice and want to go back to as if I had just done it two times, it's annoying to have to redo the whole sequence again. I know the `vim-expand-region` plugin mentioned in another answer has this, but I wonder if it's possible in vanilla vim. – smheidrich Jul 20 '21 at 22:17
  • 1
    @smheidrich: I don't know, you might want to post this as a new question. – Thor Jul 21 '21 at 13:43
8

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.

Ingo Karkat
  • 22,638
  • 2
  • 45
  • 58
1

Just updating this answer in 2020, hopefully someone finds this useful. If you use coc.nvim Which I hope you use to add Intellisense in vim/neovim.

Taken from it's readme you can bind Ctrl-S to expand selection

" Use CTRL-S for selections ranges.
" Requires 'textDocument/selectionRange' support of LS, ex: coc-tsserver
nmap <silent> <C-s> <Plug>(coc-range-select)
xmap <silent> <C-s> <Plug>(coc-range-select)

So you just press Ctrl-s and it'll expand the selection to current scope, then invoking it repeatedly just expands one scope higher. As of writing, this only works with Typescript/JavaScript.

chriz
  • 111
  • 2