Keyboard shortcut to expand selection one method below

Answered

Is there a combination of keyboard shortcuts allowing to select a method, and expand the selection below, method by method?

‘Expand selection’ is not what I want (expanding with Ctrl+W first selects on method, then the whole class body, while I want just a part of the methods).

I was thinking about using multi-cursors (https://www.jetbrains.com/help/idea/multicursor.html) but it feels really complicated in terms of different combinations needed and not really a good application of multi-cursors.

In vim, it's nice it has a built-in way to mark ‘start of selection’ and ‘end of selection’ lines. I would use the visual mode where moving ‘cursor’ moves the selection: Esc, press ‘v’, move down (using arrows, PgDn, or commands like ‘10j’ for 10 lines), press ‘d’ when I'm ready to cut the selected text.

In IntelliJ, it's not quite the same, because it's very stressful: if I accidentally press a wrong key while expanding the selection, I lose all my selection hard work. If I accidentally press an arrow key, the selection is lost. If I accidentally press a letter, the selection is lost and what's worse replaced with an unwanted text. I had to expand the selection remembering to hold the Shift key all the time, it's not very ergonomic for the hand. The selection is not very precise as I can only move either by pressing the down arrow very fast, or jump by a a whole page with the Page Down key.

Why do I need this? Frequently, I split large files (mostly test files), first by grouping related methods adjacent to each other, selecting them (usually 3 to 5 methods), cutting, and moving to a different file. Methods are of variable height, so Page Down is not very precise - it jumps to an unknown code location and I need to use the mouse scroll to see if the jump was by one method, or by too many methods.

What may be useful for this use case is something like in vim: marking ‘start of selection’ and ‘end of selection’, with ‘end of selection’ easy to be moved. I was thinking of using the bookmarks feature, but I fail see how they connect to text selection.

0
3 comments

Hi Piotr,

Two ways to go about it that I've managed to find:

  1. Assign a shortcut in Keymap settings to the action called “Toggle Sticky Selection”, enable it and jump between method names with Alt + Arrow Up/Down, then disable sticky selection mode when done.
  2. Assign a shortcut to the action called “Move Caret Forward/Backward a Paragraph with Selection” and jump through methods paragraph by paragraph. Especially useful on methods with no line breaks in them.
0

Hi Arina. Thanks for the response!

‘Sticky selection’ works great. Moving by paragraphs also works, but as you mentioned, empty lines inside methods make the navigation less effective.

I think you should advertise those features more, as they can be very useful. I searched the docs for ‘sticky selection’, but it has just one accidental mention.

I sometimes learn new things about IntelliJ from short videos published by @intellijidea on Twitter. Maybe you could also advertise those features there.

0

Glad I could help Piotr.

You are right, the feature is barely discoverable. We have an open task to add an alias for it and also update the documentation: 

You are welcome to upvote it.

0

Please sign in to leave a comment.