w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Move to the beginning of next code block in Vim

It seems you are defining a “code block” to be { } that contain at least one line. You can most easily search for those just by searching for a { at the end of a line:


/{ means search for a {, and $ represents an anchor to the end of the line.

There might be cases where a { opens a block, but is not the last character of a line:

(some_other_funtion($_)) { # this while is very important

To take this into account, do the following search for a { that is not closed on the same line:

  1. / – search for
  2. { – a { character
  3. [^}] – followed by any character that is not a }
  4. * – repeated 0 or more times
  5. $ – until the end of the line

(Vim regexes are not always the same as in Perl, but this particular one is.)

You could define a mapping for that second search by putting this in your .vimrc:

noremap <Leader>nb /{[^}]*$<CR>

That would let you jump to the next block by pressing <Leader> ( by default) n b.

Since it uses :noremap, it affects Select mode too. You won’t want that if your <Leader> is a printable character (which it is by default). In that case, add the line sunmap <Leader>nb below the previous line to fix Select mode.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.