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

6,479

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.

Solution 3

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.

Share:
6,479

Related videos on Youtube

palaniraja
Author by

palaniraja

practicing the art of programming

Updated on September 18, 2022

Comments

  • palaniraja
    palaniraja over 1 year

    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
    palaniraja about 11 years
    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]
  • Thor
    Thor about 11 years
    @palaniraja: yes there is, you can prepend the text-object by a number, e.g.: v+2+a+[.
  • smheidrich
    smheidrich almost 3 years
    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.
  • Thor
    Thor almost 3 years
    @smheidrich: I don't know, you might want to post this as a new question.