0

I'm trying to re-do the C-w selection shortcut in Jetbrains's IDE.

Is there a way to select visually the whole containing block ?

for now, i use

nnoremap <C-w> <Esc>viw
vnoremap <C-w> <Esc>{v}

Which allow me to select current word, then current block. (That's already really nice though)

I know i could select multiple blocks with like {{v}}, but that would just catch adjacent blocks, and would not backtrack to parent block, and that plays poorly with new lines.

Is there a way to achieve that ?

  • 2
    Are you looking for vip ? See :h text-objects. Also I would advise to be careful remapping <C-w> because it is a useful prefix for a lot of window related commands. And also you don't need to add <Esc> in your normal mode mapping since you are already in normal mode. – statox Feb 26 at 11:06
  • I don't think there is a built-in way to do what you want. If none of the text objects mentioned in :h text-objects fit your needs, maybe you could have a look at this plugin which allows you to create your own. – statox Feb 26 at 11:14
  • @statox thank's for the c-w advice Its a shame we cant map control shift tab vs control tab. vip looks like what I want to achieve, but i mean some "recursive vip", if that would make sense ? It seems like <Esc>{v} makes a better job selecting a whole block. But it goes too far now that I check it with a better language to test it against. Maybe I have to think about a way to go through repetition viw -> vV -> vip -> <Esc>{v}. The second mapping is a visual-mode one :p. – Pierre-Antoine Guillaume Feb 26 at 11:14
3

A slight change of workflow would be to type [{ as many times as necessary to jump to the start of the enclosing block, then V% to visually select that entire block.

See :help [{ and :help % for more details.

You can use these to implement your mappings:

nnoremap <C-W> [{V%
xnoremap <C-W> "_y[{V%

The visual mode mapping works by first yanking into the black hole register (see :help quote_), in order to exit visual mode leaving the cursor at the start of the previously selected block, and then selecting the next surrounding block with the same commands. I'm using xnoremap instead of vnoremap because the latter also applies in select mode, which you probably don't want.

Like statox, I'd recommend against using <C-W> for the lhs. You'd be better off finding another trigger for your mappings.

  • I'm letting the ticket open until tomorrow. What I synthetized is %[{v]} – Pierre-Antoine Guillaume Feb 26 at 11:28
  • @PierreAntoineGuillaume See my edit for a mapping solution. – Rich Feb 26 at 11:31
  • So now you are using a _ registry, that you yank ? What does that mean ? – Pierre-Antoine Guillaume Feb 26 at 11:31
  • @PierreAntoineGuillaume See my edit for an explanation. – Rich Feb 26 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.