Search code examples
visual-studio-codekeyboard-shortcuts

Visual Studio Code shortcut to come out of the current or enclosing block of code


Is there any shortcut in visual studio code to come out of the current block of code. For example -

while (!q.isEmpty()) {
    Oranges obj = q.pollFirst();
    ans = Math.max(obj.time, ans);
    for (int[] d : neighbors) {
        int r = obj.x + d[0];
        int c = obj.y + d[1];
        int t = obj.time + 1;//Cursor position
        if (r >= 0 && r < grid.length && c >= 0 && c < grid[0].length && grid[r][c] == 1) {
            q.offer(new Oranges(r, c, t));
            grid[r][c] = 2;
        }
    }//Target Position
}

In code above, suppose my cursor is at Cursor position and I want to come directly out of the current block of code or enclosing for-loop to Target Position. If there is any extension that can do it then please suggest.


Solution

  • There is a keyboard shortcut called "Go to Bracket" (editor.action.jumpToBracket) that gets you close to what you want. Its default keys are Ctrl + Shift + \.

    From where you have //Cursor position, it will jump down to the inside of the brace where you have //Target Position. So:

    while (!q.isEmpty()) {
        Oranges obj = q.pollFirst();
        ans = Math.max(obj.time, ans);
        for (int[] d : neighbors) {
            int r = obj.x + d[0];
            int c = obj.y + d[1];
            int t = obj.time + 1;//Cursor position
            if (r >= 0 && r < grid.length && c >= 0 && c < grid[0].length && grid[r][c] == 1) {
                q.offer(new Oranges(r, c, t));
                grid[r][c] = 2;
            }
        <cursor will move here>}//Target Position
    }
    

    If you are quick with your arrow keys, you could navigate around your code quite fast with this once you get it down.