Search code examples
navigationvibracketsbraces

In Vi, navigate / jump to the brackets around the current block


Supposing we have some code:

var f = function(a) {
  var g = {"b" : "c",
           "m" : "n" // cursor is here
           "d" : "e",
           "h" : {
             "i" : "j",
             "k": "l"
             },
           "m" : {
             "n" : {"o":"p"}
             }
           } // want to get to here
}

What would be the most economic command in vi to navigate / jump to the closing bracket for the current scope.

Bear in mind:

  • we can't use % because we're not already on the opening brace.
  • we can't use [{ beacause it's not unmatched
  • we could use /{ and n, but this is cumbersome and requires more thinking than should be necessary.

Solution

  • ?{ <ENTER> %
    The first line of commands to go to the last opened {
    Then % to find the matching brace