Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a vim command to move the cursor to the parent _square_ bracket?

Tags:

json

vim

In vim, there are handy commands to move to the nearest enclosing left or right curly brace ([{ and ]} respectively).

However, typically when navigating JSON files, it is equally useful to be able to jump to the nearest enclosing square bracket. Yet, in the long time I have been wishing for this command, I haven't found it, either built into vim or provided by a plugin. Is it there somewhere?

like image 424
mkorvas Avatar asked Dec 31 '25 15:12

mkorvas


2 Answers

How about va[ when you are inside the []. This will visually highlight the enclosed []. Esc takes the cursor on the closing ].

To go to opening [, Press %.

Or to shorten it, map it to a key of choice, eg to F2 and save it in vimrc

nnoremap <F2> va[<esc>%
like image 55
dlmeetei Avatar answered Jan 04 '26 12:01

dlmeetei


I have the following two mappings in my vimrc

nnoremap ]b :call searchpair('\[','','\]')<cr>
nnoremap [b :call searchpair('\[','','\]','b')<cr>

compare also :h searchpair. You can change ']b' and '[b' to whatever you like of course.

like image 31
joemrt Avatar answered Jan 04 '26 13:01

joemrt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!